How to tell if any entities in context are dirty with .Net Entity Framework 4.0

Posted by Mike Gates on Stack Overflow See other posts from Stack Overflow or by Mike Gates
Published on 2010-04-26T15:53:59Z Indexed on 2010/04/26 16:03 UTC
Read the original article Hit count: 325

I want to be able to tell if there is any unsaved data in an entity framework context. I have figured out how to use the ObjectStateManager to check the states of existing entities, but there are two issues I have with this.

  1. I would prefer a single function to call to see if any entities are unsaved instead of looping though all entities in the context.
  2. I can't figure out how to detect entities I have added. This suggests to me that I do not fully understand how the entity context works. For example, if I have the ObjectSet myContext.Employees, and I add a new employee to this set (with .AddObject), I do not see the new entity when I look at the ObjectSet and I also don't see the .Count increase. However, when I do a context.SaveChanges(), my new entity is persisted...huh?

I have been unable to find an answer to this in my msdn searches, so I was hoping someone here would be able to clue me in.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about entity-framework-4

Related posts about entity-framework