嵌套实体集合

Nested entity collection

我有一个场景,我正在尝试为特定目的创建一个相同类型的嵌套集合的可增长层次结构,并且我正在使用 EF Core 2.2 .

public class Group:Entity
{
    public Group(Guid id):base(id)
    {
    }
   ...
   public List<Group> SubGroups { get; set; }
}

public abstract class Entity
{
   protected Entity(Guid id)
    {
        Id = id;
    }

    public Guid Id { get; private set; }
}

目标是像这样保存数据:

|-GrandParent Group
   -Parent Group
   |--Child1 Group
      ---GrandChild1 Group
   |--Child2 Group
       ---GrandChild2 Group

错误

{System.InvalidOperationException: No suitable constructor found for entity type 'Group'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'guid' in 'Group(Guid guid)'.
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConstructorBindingConvention.Apply(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)

你能告诉我如何实现吗?

该问题与嵌套集合无关,与实体构造函数有关,并且未与问题示例一起重现。

但是异常信息

No suitable constructor found for entity type 'Group'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'guid' in 'Group(Guid guid)'.

表示在你的真实代码中使用过

public Group(Guid guid):base(guid)

问题是参数 guid(而不是 id)的 name。正如Entity types with constructors中解释的(里面一些需要注意的事情):

The parameter types and names must match property types and names, except that properties can be Pascal-cased while the parameters are camel-cased.

在这种情况下,属性 称为 Id,因此参数必须像 post 中那样称为 id