views:

314

answers:

3

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.

A: 

This article describes exactly what is needed to perform Change Tracking in the Entity Framework:

Identity Resolution, State Management, and Change Tracking (Entity Framework) - MSDN

Justin Niessner
Awesome. Don't know how I missed that. Thanks.
Mike Gates
Actually, I just read that article and it doesn't answer my question. I need to know what will happen if I do a .SaveChanges. New entities added with context.AddObject do not appear in the ObjectSet<T> properties off of the context object, so I can't get a reference to them in order to check their state.
Mike Gates
+2  A: 
var addedStateEntries = Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added);
Craig Stuntz
Ok, that looks like the answer. I am still a bit confused as to why the ObjectSet myContext.Employees does not contain the entity I just added.
Mike Gates
Because `ObjectSet`s are queries, not collections.
Craig Stuntz
Ok, we're getting to my misunderstanding of what those ObjectSets are supposed to be. So, is it correct to say that those ObjectSets are just the database state of the entities? If that is true, is there a collection that, for example, has all the Employee entities that are in memory (not just those in the database)?
Mike Gates
I'll rephrase that. Is there a collection somewhere that has all the Employee entities that are in the context as opposed to just those that exist in the database?
Mike Gates
I've started a new question on that topic at http://stackoverflow.com/questions/2716359/help-me-understand-entity-framework-contexts-please
Mike Gates
A: 

A simple way to get a reusable single method/property you could add a new method to your ObjectContext by creating a partial class and adding a property like this:

 public partial class MyEntityContext
{

    public bool IsContextDirty{
        get{
            var added = ObjectStateManager.GetObjectStateEntries(EntityState.Added);
            if(added != null && added.Count() > 0){
                return true;
            }
            var deleted = ObjectStateManager.GetObjectStateEntries(EntityState.Deleted);
            if (deleted != null && deleted.Count() > 0){
                return true;
            }
            var modified = ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
            if(modified != null && modified.Count() > 0){
                return true;
            }
            return false;

        }
    }

Depending on what your looking for you could expose other properties to know if there are just deletes or modifications. This method could be simplified, but I wanted it to be clear what you would need to do.

Greg Roberts