SaveChanges() in Entity Framework not working?

c#-3.0 entity-framework

Question

I use the entity framework with Visual Studio 2008, and both the.NET Framework and VS were updated to SP1. In an n-tier environment, I developed my application. I transform data from context to collection in the first phase, then serialize it for the user.

HRMSDBContext context = new HRMSDBContext();

List<InHouseTrainingHead> heads = context.InHouseTrainingHead.ToList<InHouseTrainingHead>();

foreach (InHouseTrainingHead head in heads)
{
    context.Detach(head);
}

return heads;

After the user makes adjustments and clicks "Saving," the list returns to the save procedure.

HRMSDBContext context = new HRMSDBContext();

foreach (InHouseTrainingHead head in lists)
{
    context.Attach(head);
    context.ApplyPropertyChanges(head.EntityKey.EntitySetName, head);
}

context.SaveChanges();

Unfortunately, nothing happens after SaveChanges(). How can I fix this issue?

1
2
6/8/2018 10:09:40 AM

Accepted Answer

Zzz-5-Zzz is not being utilized as it was meant to be. A changed one separated from the context and an unmodified one tied to the context are both required instances of the entity. After the unmodified associated object receives the modifications, it is changed and you have the option to save the changes. Therefore, before calling, you must load the entity from the database rather than attach the updated object.ObjectContext.ApplyPropertyChanges .

2
7/13/2010 10:49:40 AM

Popular Answer

Daniel Bruckner's response might be expanded upon by saying that in your sample, you are attempting toApplyPropertyChanges in betweenhead a thing by itself. There are obviously no modifications, thus nothing is preserved. However, you may set the option not to get the record from the DB if you don't want to.EntityState the newly affiliating entity toModified manually. It will be kept in the database in this instance:

HRMSDBContext context = new HRMSDBContext();

foreach (InHouseTrainingHead head in lists)
{
    context.Attach(head);
    ObjectStateEntry addedEntity = context.ObjectStateManager.GetEntry(head);
    addedEntity.ChangeState(EntityState.Modified);
}

context.SaveChanges();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow