gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:16:41 26 4
gpt4 key购买 nike

我有以下模型:

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 基本上区分了两种关系:ownedassociated

Owned 可以解释为“成为...的一部分”,这意味着拥有的任何东西都将与其所有者一起插入/更新/删除。

GraphDiff 处理的另一种关系是关联的,这意味着在更新图形时,GraphDiff 只更改关联实体本身的关系,而不是关联实体本身。

当您使用AssociatedEntity 方法时,子实体的状态不是聚合的一部分,换句话说,您对子实体所做的更改不会被保存,只是它会更新父导航属性。

如果您想保存对子实体的更改,请使用OwnedEntity 方法,因此,我建议您试试这个:

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

关于c# - 如何使用 GraphDiff 更新相关实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28341027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com