首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用GraphDiff更新相关实体?

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

Stack Overflow用户
提问于 2015-02-05 10:00:58
回答 1查看 4.4K关注 0票数 7

我有以下模式:

代码语言:javascript
复制
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,我希望按以下方式更新客户聚合:

代码语言:javascript
复制
dbContext.UpdateGraph<Customer>(entity, 
            map => map.AssociatedEntity(x => x.Address)
                      .OwnedCollection(x => x.Categories, with => with.AssociatedEntity(x => x.Category)));

但是上面没有更新任何东西!!

在这种情况下,使用GraphDiff的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-05 16:16:31

GraphDiff基本上区分了两种关系:所拥有的关联的

拥有可以解释为“是其中的一部分”,意思是拥有的任何东西都将与其所有者一起插入/更新/删除。

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

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

如果要保存子实体上的OwnedEntity更改,请使用方法,因此,我建议您尝试以下操作:

代码语言:javascript
复制
dbContext.UpdateGraph<Customer>(entity,  map => map.OwnedEntity(x => x.Address)
                                                   .OwnedCollection(x => x.Categories, with => with.OwnedEntity(x => x.Category)));
dbContext.SaveChanges();
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28341027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档