我有以下模式:
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的正确方法是什么?
发布于 2015-02-05 16:16:31
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();https://stackoverflow.com/questions/28341027
复制相似问题