簡體   English   中英

如何使用GraphDiff更新相關實體?

[英]How to update related entities using GraphDiff?

我有以下型號:

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;}
}

從上面,並使用GraphDiff,我想更新客戶聚合如下:

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

但上面沒有更新任何東西!!

在這種情況下使用GraphDiff的正確方法是什么?

GraphDiff基本上區分了兩種關系: 擁有關聯

擁有可以被解釋為“成為”的一部分,意味着擁有的任何東西都將與其所有者一起插入/更新/刪除。

GraphDiff處理的另一種關系是關聯的,這意味着在更新圖形時,GraphDiff只會更改與關聯實體本身的關系,而不會更改關聯實體本身的關系。

當您使用AssociatedEntity方法時,子實體的State不是聚合的一部分,換句話說,您對子實體所做的更改將不會保存,只會更新父navegation屬性。

如果要保存對子實體的更改,請使用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