【发布时间】:2014-01-10 17:08:41
【问题描述】:
我的应用程序对每个聚合根都有一个存储库,但有时我只需要从这个聚合根中删除一个子实体。如何使用存储库模式做到这一点?
例如:
品牌(总根) |_ CarModels(汽车模型集合)我需要从品牌 B 中删除 CarModel A,但我只有 BrandRepository。那么我该如何实现呢?
我不知道我的代码是否对你有帮助,但这里是:
public class Brand
{
public int Id {get;set;}
public string Name {get;set;}
public virtual ICollection<CarModel> Models {get;set;}
}
public class CarModel
{
public int id {get;set;}
public string Name {get;set;}
}
public class BrandRepository
{
/// ....
public void Save(Brand entity)
{
this.context.Brands.AddOrUpdate(entity);
this.context.SaveChanges();
}
public void Delete(Brand entity)
{
this.context.Brands.Remove(entity);
this.context.SaveChanges();
}
}
缺少的一个信息是我使用的是 EF 代码优先。
【问题讨论】:
-
这是具体实现的问题。因此,如果没有代码,您的问题毫无意义。
-
为问题添加了更多细节。
-
@JulianoNunesSilvaOliveira,您选择了哪种解决方案?请接受答案。
标签: c# ef-code-first domain-driven-design repository-pattern aggregateroot