简体   繁体   English

如何使用GraphDiff更新相关实体?

[英]How to update related entities 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 customer aggregate as follows: 从上面,并使用GraphDiff,我想更新客户聚合如下:

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

But the above is not updating anything!! 但上面没有更新任何东西!!

What is the correct way to use GraphDiff in this case? 在这种情况下使用GraphDiff的正确方法是什么?

GraphDiff basically distinguishes two kinds of relations: owned and associated . GraphDiff基本上区分了两种关系: 拥有关联

Owned can be interpreted as "being a part of" meaning that anything that is owned will be inserted/updated/deleted with its owner. 拥有可以被解释为“成为”的一部分,意味着拥有的任何东西都将与其所有者一起插入/更新/删除。

The other kind of relation handled by GraphDiff is associated which means that only relations to, but not the associated entities themselves are changed by GraphDiff when updating a graph. GraphDiff处理的另一种关系是关联的,这意味着在更新图形时,GraphDiff只会更改与关联实体本身的关系,而不会更改关联实体本身的关系。

When you use the AssociatedEntity method, the State of the child entity is not part of the aggregate, in other words, the changes that you did over the child entity will not be saved, just it will update the parent navegation property. 当您使用AssociatedEntity方法时,子实体的State不是聚合的一部分,换句话说,您对子实体所做的更改将不会保存,只会更新父navegation属性。

Use the OwnedEntity method if you want to save tha changes over the child entity, so, I suggest you try this: 如果要保存对子实体的更改,请使用OwnedEntity方法,因此,我建议您尝试这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM