C# 编写一个私有属性,它是自己的类对象列表
我有一个类,它有一些私有属性,这些属性是它自己类型的列表。当我想在写入DB时配置实体框架时,我会得到这样的错误: 类型“ICollectionC# 编写一个私有属性,它是自己的类对象列表,c#,entity-framework,C#,Entity Framework,我有一个类,它有一些私有属性,这些属性是它自己类型的列表。当我想在写入DB时配置实体框架时,我会得到这样的错误: 类型“ICollection”必须是不可为null的值类型,才能将其用作泛型类型或方法中的参数“T” 课程为: public partial class ModelItem { public int Id { get; set; } public string Lable { get; set; } private ICollection<Model
public partial class ModelItem
{
public int Id { get; set; }
public string Lable { get; set; }
private ICollection<ModelItem> Prop_InputNodes
{
get; set;
}
public class ModelItemConfiguration : EntityTypeConfiguration<ModelItem>
{
public ModelItemConfiguration()
{
Property(x => x.Prop_InputNodes); // <<-- Error raises here
}
}
}
公共部分类ModelItem
{
公共int Id{get;set;}
公共字符串标签{get;set;}
专用ICollection Prop_InputNodes
{
获得;设置;
}
公共类ModelItemConfiguration:EntityTypeConfiguration
{
公共模型项配置()
{
属性(x=>x.Prop_InputNodes);//您应该使用集合映射方法:
public class ModelItemConfiguration : EntityTypeConfiguration<ModelItem>
{
public ModelItemConfiguration()
{
this.HasMany(x => x.Prop_InputNodes);
}
}
根据错误,Property
是一种预期使用不可为空的值类型(例如int
或double
)调用的方法。您没有传递不可为null的值类型。那么我如何在db中保存Prop_InputNodes?该类也有一个Prop_OutputNodes
,因此我将其添加到配置中,如HasMany(x=>x.Prop_InputNodes)。with many(y=>y.Prop_OutputNodes);
。希望它能帮助将来的人。
this.HasMany(x => x.Prop_InputNodes)
.WithOptional()
.Map(m => m.MapKey("ParentID"));