使用自定义集合类型作为后备字段

时间:2019-11-11 22:14:33

标签: entity-framework-core

在Entity Framework Core 3中是否可以将自定义集合类型作为后备字段?

我有一个与实体B有OwnsMany关系的实体A。由于在将B添加到A之前需要检查许多业务规则,因此我必须依靠AddB,{{ 1}}和GetB以便执行此规则。

RemoveB

在这里,随着管理B的业务规则的出现,A类越来越多地使用诸如 public class Id { ... } public class B{ private A _a; internal B(A a){ _a = a; } private B() { _a = default!; } } public class A { private List<B> _bs; public IReadOnlyCollection<B> Bs => _bs.AsReadOnly(); public B AddB() { // validate stuff var b = new B(this); _bs.Add(b); return b; } public B GetB(Id id) { //some linq to find B or throw if id not found } public B? FindB(Id id) { return null if id not found } public void RemoveB(B b) => _bs.Remove(b); } 之类的方法,其中大多数是linq查询或对集合执行计算的方法。而且,如果我添加了另一个类DoSomethingForB的集合,则A的复杂性会增加。因此,我想委托linq查询以及CGet方法来收集类,而不是依赖于通用的Find方法。

List<T>

但是,我不确定这是否是EF Core 3上受支持的方案,特别是如果EF Core 3会知道如何重构我的 public class Id { ... } public class B{ private A _a; internal B(A a){ _a = a; } private B() { _a = default!; } } public class BCollection : IEnumerable<B> { private List<B> _items; public B GetB(Id id) { //some linq to find B or throw if id not found } public B? FindB(Id id) { return null if id not found } internal void Add(B b) => _items.Add(b); // add some other methods like Remove etc. } public class A { private BCollection _bs; public BCollection Bs => _bs; public A() { _bs = new BCollection(); } public B AddB() { // validate stuff var b = new B(this); _bs.Add(b); return b; } } 类型,或者我是否必须遵循一些特定规则(也许实施一个接口或在BCollection配置中进行一些特殊配置)。

谢谢。

0 个答案:

没有答案