How to update related objects using GraphDiff?

I have the following 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;} } 

From the above and using GraphDiff, I want to update the client aggregate as follows:

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

But nothing is updated above !!

What is the correct way to use GraphDiff in this case?

+6
source share
1 answer

GraphDiff basically distinguishes between two kinds of relationships: owned and .

Own can be interpreted as "being part", meaning that everything that belongs to it will be inserted / updated / deleted with its owner.

Another kind of relationship handled by GraphDiff is related, which means that only changes to related objects, and not related entities, change GraphDiff when the chart is updated.

When you use the AssociatedEntity method, the state of the child is not part of the population, in other words, the changes you made to the child will not be saved, it will simply update the parent non-renew property.

Use the OwnedEntity method if you want to save the changes to a child, so I suggest you try the following:

 dbContext.UpdateGraph<Customer>(entity, map => map.OwnedEntity(x => x.Address) .OwnedCollection(x => x.Categories, with => with.OwnedEntity(x => x.Category))); dbContext.SaveChanges(); 
+6
source

Source: https://habr.com/ru/post/982030/


All Articles