zzzprojects / GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
https://entityframework-graphdiff.net/overview
MIT License
333 stars 102 forks source link

How to update OwnedCollection with multiple associated entities? #138

Closed cryo75 closed 9 years ago

cryo75 commented 9 years ago

Is it possible to update such a graph?

ghost commented 9 years ago

Hi,

I'm not exactly sure about the relations in your scenario: do you have multiple associated entities or do you have a single collection of multiple associated entities?

Anyways, it should work like this for the first case (more than one navigation property):

var attachedRoot = context.UpdateGraph(changedRoot,
    a => a.OwnedCollection(b => b.OwnedCollection,
        with => with.AssociatedEntity(d => d.FirstAssociate)
                    .AssociatedEntity(d => d.SecondAssociate)));

And like this for the second case (the navigation property is a collection):

var attachedRoot = context.UpdateGraph(changedRoot,
    a => a.OwnedCollection(b => b.OwnedCollection,
        with => with.AssociatedCollection(d => d.AssociatedCollection)));
cryo75 commented 9 years ago

Thanks. It is the first case.

But I don't understand the second case.

ghost commented 9 years ago

Hi,

in the second case you'd have multiple associated entities of the same type contained within a single collection. The classes would look something like this:

public class Root
{
    // [..] Id (Primary Key) etc.

    public List<OwnedEntity> OwnedCollection { get; set; }
}

public class OwnedEntity
{
    // [..] Id (Primary Key) etc.

    public List<AssociatedEntity> AssociatedCollection { get; set; }
}

public class AssociatedEntity
{
    // [..] Id (Primary Key) etc.
}