0 Comments

When you disable state tracking in Entity Framework 4.3, for performance improvements (and you know what you’re doing Winking smile), by setting: DbContext.Configuration.AutoDetectChangesEnabled = false , then you are responsible for your own state tracking on entities.

 

The process of updating an entity, involves 3 steps:

1. Setting the EntityState to Modified.

2. Save changes to database.

3. Setting the EntityState to Unchanged.

 

Code

// Set entity to modified, so the entity will be updated in the database.
_dbContext.Entry(imageInfo).State = System.Data.EntityState.Modified;

// Save changes to database.
_dbContext.SaveChanges();

// Set entity to unchanged, so the entity will be not be updated in future calls to _dbContext.SaveChanges().
_dbContext.Entry(imageInfo).State = System.Data.EntityState.Unchanged;    

Notes

1. Where _dbConext is an instance of the DbContext class.

2. If you don’t set the Entity.State to Modified, no changes will be persisted to the database.

3. If you don’t set the Entity.State to Unchanged after SaveChanges(), future calls to SaveChanges() will repeat the update for this entity.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts