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;
if(original == null)
{
context.AddObject("ApplicationUsers",au);
}
else
{
original.SSOID = au.SSOID;
ObjectStateEntry entry = null;
context.DetectChanges();
context.ObjectStateManager.TryGetObjectStateEntry(original.EntityKey, out entry);
context.ApplicationUsers.ApplyCurrentValues(original);
}
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" />
<ScalarProperty Name="CreatedOn" ColumnName="CreatedOn" />
<ScalarProperty Name="CreatedBy" ColumnName="CreatedBy" />
<ScalarProperty Name="SSOID" ColumnName="SSOID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>