Entity framework: check if there are changes to be saved from a specific Entity
I want to detect if changes where made to a specific set of entity before saving.
I am currently using this method but it returns true if there are any Entity modified in the context.
const EntityState ModifiedID = EntityState.Modified
| EntityState.Added
| EntityState.Deleted;
var objectStateEntries = Database.LabelTAB
.Context.ObjectStateManager
.GetObjectSt开发者_开发百科ateEntries(ModifiedID);
return objectStateEntries.Any();
Is there any way to detect if there are some unsaved entries in the LabelTAB
entity only, and not in the entire Context
?
Thank you.
Try this:
var objectStateEntries = Database.LabelTAB
.Context
.ObjectStateManager
.GetObjectStateEntries(ModifiedID)
.Where(e => e.Entity is LabelTAB);
return objectStateEntries.Any();
Use:
var states = new List<EntityState>() { EntityState.Modified,
EntityState.Added,
EntityState.Deleted};
var query = from t in Database.LabelTAB
where states.Contains(t.EntityState);
Each entity has an EntityState
property. Instead of querying the ObjectContext you can just use the property on the entity.
精彩评论