Michael Faisst
Michael Faisst

Reputation: 607

Concurrency with GraphDiff when adding different child objects to owned collection

to make things not too complex, I will not take my own entity model for this question but the one from the original GraphDiff-Blog-Post here where there is a company entity that has multiple contacts. To illustrate my problem I think this is more than enough.

So basically my code is the same as in the graphdiff example, which works fine:

using (var context = new TestDbContext())  
{
    // Update the company and state that the company 'owns' the collection Contacts.
    context.UpdateGraph(company, map => map
        .OwnedCollection(p => p.Contacts)
    );

    context.SaveChanges();
}

The problem is when trying to add new contacts to the same company on 2 different instances of the program. For example, Instance 1 saves a new Contact A, afterwards Instance 2 saves a new Contact B. The Graph from Instance 1 gets updated in the DB just fine, but the graph from Instance 2 overrides all the changes that Instance 1 made and deletes the newly created Contact A.

Is there a way to solve this problem with Graphdiff? Maybe I need to make 2 indipendent updates(one for company and one for contacts), but using GraphDiff in the first place wouldn't make much sense anymore. Also thought about making a diff before updating the graph, but that seems pretty overkill as well.

Or maybe I'm just to unexperienced with entity framework all together to see the easy solution : )

Any help is appreciated!

Cheers, Michael

Upvotes: 0

Views: 195

Answers (0)

Related Questions