How to update related entities using GraphDiff?

c# ef-code-first entity-framework graphdiff

Question

I possess the next model:

public class Customer
{
    public int Id {get; set;}
    public string Name {get; set;}
    public int AddressId {get; set;}
    public virtual Address Address {get; set;}
    public virtual ICollection<CustomerCategory> Categories {get; set;}
}

public class CustomerCategory
{
    public int Id {get; set;}
    public int CustomerId {get; set;}
    public int CategoryId {get; set;}
    public virtual Category Category {get; set;}
}

public class Address
{
    public int Id {get; set;}
    public string Street{get; set;}
    public virtual PostCode PostCode {get; set;}
}

In light of the aforementioned, I wish to update the customer aggregate as follows using GraphDiff:

dbContext.UpdateGraph<Customer>(entity, 
            map => map.AssociatedEntity(x => x.Address)
                      .OwnedCollection(x => x.Categories, with => with.AssociatedEntity(x => x.Category)));

However, nothing has updated as of yet!

What is the appropriate application of GraphDiff in this scenario?

1
7
2/5/2015 10:00:58 AM

Accepted Answer

The two types of relations that GraphDiff fundamentally identifies are owned and associated.

Owned can be understood as "being a part of," which means that everything owned will be added to, modified, or removed together with its owner.

The other type of relation handled by GraphDiff is an associated relation, which means that when updating a graph, GraphDiff changes just the relations to the related entities—not the associated entities themselves.

When utilizing theAssociatedEntity state of the child entity is not included in the aggregate, thus any changes you make to the child entity won't be stored; instead, the parent navigation property will be updated.

Apply theOwnedEntity method Try the following if you want to save the modifications to the child entity:

dbContext.UpdateGraph<Customer>(entity,  map => map.OwnedEntity(x => x.Address)
                                                   .OwnedCollection(x => x.Categories, with => with.OwnedEntity(x => x.Category)));
dbContext.SaveChanges();
8
8/25/2016 11:53:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow