7

我有以下模型:

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 的正确方法是什么?

4

1 回答 1

11

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

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

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();
于 2015-02-05T16:16:31.837 回答