8

I have the current scenario:

I'm using EF6 Code first, and have created a data-model something like this:

public class MainObject{
  ..some properties
  IList<SubObject> SubObjects{get;set;}
}

public class SubObject{
  ..some properties
  IList<SubSubObject> SubSubObjects{get;set;}
}

public class SubObject{
  ..some properties
  IList<SubObject> SubObjects{get;set;}
}

So basically I have a main object, that has 0 to many subobjects, and there is a many to many relationship between the subobject and the subsubobjects.

I am creating a MVC application, so my normal program flow is that the user request a page that basically uses a MainObject as it's data model. Then the user interacts with the page and changes, adds or removes subobjects and subsubobjects as he wishes, and then clicks save. On save, the objectgraph is sent back to the controller and it looks correct according to the changes done by the user on the client side. Now my problem is the following:

How to store this back into the database in a good fashion.

I need to attach my object back into the context, but I have no clue which objects are new, modified or deleted.

I have written some code that partially works now, but it's getting so ugly that I really don't want to go down that path. Would it be possible somehow to fetch the relevant object graph from the database, and have EF compare the two graphs toward eachother, and then save the relevant changes to the database?

Any help to make this smoother would be greatly appreciated.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • You may want to try [GraphDiff](https://github.com/refactorthis/GraphDiff). I never tried it, but it looks promising. – Gert Arnold Feb 10 '14 at 21:43
  • @GertArnold - Actually, I tried it last evening, and it solved my issue in a really good fashion. So highly recommended. If you like to make that comment into an answer, I'll be happy to mark it as accepted :) – Øyvind Bråthen Feb 11 '14 at 06:17
  • Would you mind answering your own question (which is OK at StackOverflow) and briefly showing what you did? Compared to me, you're the expert now! I'll be happy to upvote. – Gert Arnold Feb 11 '14 at 07:35
  • @GertArnold - Probably a good idea :) Done. – Øyvind Bråthen Feb 11 '14 at 07:54

1 Answers1

8

I ended up using GraphDiff to solve this for me, and it works just great! This really should be built into EF, but untill it does, this is a great substitute.

To solve the example given in my question above, this will make sure that the detached graph gets saved properly (given I have a MainObject I want to save called main):

context.UpdateGraph(main, map =>map
  .AssociatedCollection( m => m.SubObjects, with => with
    .AssociatedCollection( s => s.SubSubObjects)
  )
);

context.SaveChanges();
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • I actually have the same issue, but I am unsuccessful in my implementation of GraphDiff thus far? Could you perhaps provide some more information about your EF setup? The version you are using, whether change tracking is enabled, etc? Perhaps that could assist in resolving the issue for me. http://stackoverflow.com/questions/22098433/entity-framework-duplicates-lookup-values-when-assigned-to-nested-list-member –  Mar 02 '14 at 22:17
  • This is amazing! And I'd love to use it, but I can't find the same functionality for EF Core. Does anyone know about an EF core version? – Newteq Developer Feb 14 '22 at 10:50
  • did anyone ever find an alternative to GraphDiff for the core version?? I'm stuck with the same issue regards a complex object graph that can have any number of additions/deletes and amends within the same transaction and would love a robust, simple one line solution, such as the GraphDiff library – jim tollan Apr 25 '23 at 09:07