none
Attaching an object graph with m-m RRS feed

  • Question

  • Hi,

    I'm attaching an object graph to a context in a client-server environment.  I 'Add' the root of the graph, and recurse my way through the graph adjusting entity states and setting properties dirty as needed.  All is working well except for the m-m relationships.  

    If I 'Add' these, and then go back and modify the state of the objects on both sides of the m-m relationship to "Unchanged", when I call "SaveChanges", I get a duplicate key insertion exception ("Cannot insert duplicate key in object 'dbo.TheJoinTable'") on the join table of the m-m relationship.

    1. I am assuming this is because the relationship is still in an "Added" state.  Correct?

    2. I cannot get ChangeRelationshipState calls to make a difference.  Doing something like:

    objectContext.ObjectStateManager.ChangeRelationshipState(obj1, obj2, "obj2s", EntityState.Unchanged)

    Can someone enlighten me, or give me some ideas on how to fix this?

    TIA


    WNC

    Tuesday, October 16, 2012 9:47 AM

Answers

  • Hi,

    You can access the relationship and change its state, but you need to drop to ObjectContext in order to do it.

    If you are using DbContext then you can access the ObjectContext by doing the following:

    var objectContext = ((IObjectContextAdapter)db).ObjectContext;

    This assumes you have an instance of your DbContext called db.

    Once you have ObjectContext you can use ObjectStateManager.ChangeRelationshipState to change the relationship in the way that you wanted.

    If you are using ObjectContext directly, without a DbContext, then you can just call ObjectStateManager directly.Documentation on the state manager is here: http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx

    One note about using attach instead of Add is that if you call attach and you are adding (as in creating new rows) more than one of the same object in the graph, then you will get an exception trying to call attach. This is because they will both have a primary key of zero until they are inserted into the database, unless you are not using database generated keys. So calling Attach instead of Add may also work for you, depending on your scenario but it fails for people frequently due to the primary key violation.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.




    Thursday, October 18, 2012 4:52 PM
    Moderator

All replies

  • If you want to context track some existing entities, you shouldn't use 'Add' method to add the entities to the context, you need to use 'Attach' method.

    Go go Doraemon!

    Thursday, October 18, 2012 3:13 AM
  • Hi,

    You can access the relationship and change its state, but you need to drop to ObjectContext in order to do it.

    If you are using DbContext then you can access the ObjectContext by doing the following:

    var objectContext = ((IObjectContextAdapter)db).ObjectContext;

    This assumes you have an instance of your DbContext called db.

    Once you have ObjectContext you can use ObjectStateManager.ChangeRelationshipState to change the relationship in the way that you wanted.

    If you are using ObjectContext directly, without a DbContext, then you can just call ObjectStateManager directly.Documentation on the state manager is here: http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx

    One note about using attach instead of Add is that if you call attach and you are adding (as in creating new rows) more than one of the same object in the graph, then you will get an exception trying to call attach. This is because they will both have a primary key of zero until they are inserted into the database, unless you are not using database generated keys. So calling Attach instead of Add may also work for you, depending on your scenario but it fails for people frequently due to the primary key violation.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.




    Thursday, October 18, 2012 4:52 PM
    Moderator