Entity framework 嵌套实体集合
我有一个场景,在这个场景中,我试图为一个特定的目的建立一个相同类型的嵌套集合的可扩展层次结构,并使用EF Core 2.2Entity framework 嵌套实体集合,entity-framework,entity-framework-core,ef-core-2.2,Entity Framework,Entity Framework Core,Ef Core 2.2,我有一个场景,在这个场景中,我试图为一个特定的目的建立一个相同类型的嵌套集合的可扩展层次结构,并使用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(
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; }
}
错误
{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)
你能告诉我怎样才能做到这一点吗 该问题与嵌套集合无关,而是与实体构造函数有关,并且不会与问题中的示例一起重现 但是异常消息 未找到实体类型“组”的合适构造函数。以下构造函数的参数无法绑定到实体类型的属性:无法在“组(guidguid)中绑定“guid” 表示您在实际代码中使用了
public Group(Guid guid):base(guid)
问题是参数guid
(而不是id
)的名称。如中所述(内部有一些需要注意的事项):
参数类型和名称必须与属性类型和名称匹配,但属性可以是Pascal大小写,而参数可以是驼峰大小写
在这种情况下,该属性被称为Id
,因此该参数必须像在post中一样被称为Id
public Group(Guid guid):base(guid)