Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编写一个私有属性,它是自己的类对象列表_C#_Entity Framework - Fatal编程技术网

C# 编写一个私有属性,它是自己的类对象列表

C# 编写一个私有属性,它是自己的类对象列表,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

我有一个类,它有一些私有属性,这些属性是它自己类型的列表。当我想在写入DB时配置实体框架时,我会得到这样的错误:

类型“ICollection”必须是不可为null的值类型,才能将其用作泛型类型或方法中的参数“T”

课程为:

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"));