EF 4.0 POCO magic does not work - no changes detected

I cannot update my database from disconnected poco objects. In this example, I retrieve the ApplicationUser object, update the SSOID field of varchar (100), but no changes take effect. Even when I return the object in the save method, nothing is sent to db.

If I try to call ApplyCurrentValues, it throws

An object with a key that matches the key of the provided object cannot be found in ObjectStateManager. Make sure that the key values ​​of the supplied object correspond to the key values ​​of the object to which the changes should apply.

 public void Save(ApplicationUser au)
 {
     ApplicationUser original = context.ApplicationUsers.FirstorDefault(a => a.UserID == au.UserID);
     context.ContextOptions.ProxyCreationEnabled = false;  //changing this has no effect

     if(original == null)
     {
         context.AddObject("ApplicationUsers",au); //this works!
     }
     else
     {
          ///let manually set some properties on the object I just fetched.
          original.SSOID = au.SSOID;
          ObjectStateEntry entry = null;
          context.DetectChanges();
          context.ObjectStateManager.TryGetObjectStateEntry(original.EntityKey, out entry);
          //entry is still null!
          context.ApplicationUsers.ApplyCurrentValues(original);  //BOOM!!
      }
      context.SaveChanges();
      return;
  }

I tried everything that I could come up with, even doing ApplicationUserimplement System.Data.Objects.DataClasses.IEntityWithKey, which, presumably, is not needed.

Here's the mapping:

<EntitySetMapping Name="ApplicationUsers">
    <EntityTypeMapping TypeName="MyCompanyName.ApplicationUser">
         <MappingFragment StoreEntitySet="ApplicationUser">
            <ScalarProperty Name="UserID" ColumnName="UserID" />
            <ScalarProperty Name="SystemID" ColumnName="SystemID" />
            <ScalarProperty Name="Username" ColumnName="Username" />
            <!--snip-->
            <ScalarProperty Name="CreatedOn" ColumnName="CreatedOn" />
            <ScalarProperty Name="CreatedBy" ColumnName="CreatedBy" />
            <ScalarProperty Name="SSOID" ColumnName="SSOID" />
         </MappingFragment>
     </EntityTypeMapping>
</EntitySetMapping>
+3
1

/ POCO T4 ?

POCO - , .

. :

: , - . ( , , -, , ).

, , , / au original; original , context.SaveChanges().

, .DetectChanges() , POCO - , . au , - .

+2

Source: https://habr.com/ru/post/1769358/


All Articles