I've read through LOTS of posts on saving a detached entity in Entity Framework. All of them seem to apply to older versions of Entity Framework. They reference methods such as ApplyCurrentValues and ChangeObjectState which do not seem to exist. On a whim I decided to try a method I found through intellisense and I want to make sure this is the correct way to do this since I don't get to see what happening behind the scenes:
public void SaveOrder(Order order) { using (VirtualWebEntities db = new VirtualWebEntities()) { db.Orders.Attach(order); db.Entry(order).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
Is this the correct way to update an existing item that was changed?
If you want to detach an object that is already attached to the context, set the state to Detached . If you want to load entities from the DB without attaching them at all to the context (no change tracking), use AsNoTracking . @kjbartel : this is the expected behavior, since the entity has no link with the context.
Insert Data Add methods add a new entity to a context (instance of DbContext) which will insert a new record in the database when you call the SaveChanges() method. In the above example, context. Students. Add(std) adds a newly created instance of the Student entity to a context with Added EntityState.
Detached is the default state of a newly created entity because the context can't track the creation of any object in your code. This is true even if you instantiate the entity inside a using block of the context. Detached is even the state of entities retrieved from the database when tracking is disabled.
Following are the two steps that needs to be taken with disconnected entity graph or even a single disconnected entity. Attach entities with the new context instance and make context aware about these entities. Set appropriate EntityStates to these entities manually.
Yes, this is correct. This article describes various ways of adding and attaching entities, and it provides this example:
var existingBlog = new Blog { BlogId = 1, Name = "ADO.NET Blog" }; using (var context = new BloggingContext()) { // The next step implicitly attaches the entity context.Entry(existingBlog).State = EntityState.Modified; // Do some more work... context.SaveChanges(); }
Since EF doesn't know which properties are different from those in the database, it will update them all:
When you change the state to Modified all the properties of the entity will be marked as modified and all the property values will be sent to the database when SaveChanges is called.
To avoid this, you can set which properties are modified manually rather than setting the entire entity state:
using (var context = new BloggingContext()) { var blog = context.Blogs.Find(1); context.Entry(blog).Property(u => u.Name).IsModified = true; // Use a string for the property name context.Entry(blog).Property("Name").IsModified = true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With