Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework attach update not working

I'm trying to update a POCO object using entity framework in the following way:

 context.Jobs.Attach(job);
 context.SaveChanges();

That does not work. No error is thrown, it just isn't updating the values in the database.

I tried:

context.Jobs.AttachTo("Jobs", job);
context.SaveChanges();

Nothing wrongs, still no error and no updates.

like image 493
Shawn Mclean Avatar asked Jan 18 '11 19:01

Shawn Mclean


People also ask

How do I update data in Entity Framework?

Update Objects in Entity Framework 4.0 The steps to update an existing entity are quite simple. First retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then edit the properties of the Entity and finally call SaveChanges() on the context.

What is attach in EF core?

Attach is used when you know that an entity already exists in the database but want to make some changes while change state to modified when you have already made the changes.

What is EntityState in Entity Framework?

The Entity state represents the state of an entity. An entity is always in any one of the following states. Added: The entity is marked as added. Deleted: The entity is marked as deleted.

What is EntityState detached?

An entity is in this state immediately after it has been created and before it is added to the object context. An entity is also in this state after it has been removed from the context by calling the Detach(Object) method or if it is loaded by using a NoTrackingMergeOption.


1 Answers

What about changing the ObjectState?

context.ObjectStateManager.ChangeObjectState(job, System.Data.EntityState.Modified);

From MSDN: ObjectStateManager.ChangeObjectState Method.

like image 126
CD.. Avatar answered Oct 04 '22 03:10

CD..