Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework 实体框架中的代码优先映射_Entity Framework_Entity Framework 6_Code First_Foreign Key Relationship - Fatal编程技术网

Entity framework 实体框架中的代码优先映射

Entity framework 实体框架中的代码优先映射,entity-framework,entity-framework-6,code-first,foreign-key-relationship,Entity Framework,Entity Framework 6,Code First,Foreign Key Relationship,我使用代码优先的方法创建了3个表。当我在student表上执行Find时,我得到以下模型验证异常 Student_courses_Target_Student_courses_Source::关系约束中从属角色和主体角色中的属性数必须相同 公立大学 { [关键] 公共字符串Uni_ID{get;set;} 公共虚拟列表课程{get;set;} } 公共课 { [关键] [第列(顺序=1)] 公共字符串Course_ID{get;set;} [钥匙,外国钥匙(“uni”)] [第列(顺序=2)]

我使用代码优先的方法创建了3个表。当我在student表上执行Find时,我得到以下模型验证异常

Student_courses_Target_Student_courses_Source::关系约束中从属角色和主体角色中的属性数必须相同

公立大学
{
[关键]
公共字符串Uni_ID{get;set;}
公共虚拟列表课程{get;set;}
}
公共课
{
[关键]
[第列(顺序=1)]
公共字符串Course_ID{get;set;}
[钥匙,外国钥匙(“uni”)]
[第列(顺序=2)]
公共字符串Uni_ID{get;set;}
公共虚拟ICollection学生{get;set;}
公共虚拟大学uni{get;set;}
}
公立班学生
{
[键,外键(“课程”),列(顺序=1)]
公共字符串Course_ID{get;set;}
[外键(“课程”)栏(顺序=2)]
公共字符串Uni_ID{get;set;}
[关键]
[第列(顺序=3)]
公共字符串Student_ID{get;set;}
公共虚拟课程{get;set;}
}

据我所知,异常意味着我没有将student表中的外键映射到course表中的主键。但我已经做到了。“Uni_ID”在大学表和课程表中是如何作为主键出现的,可能是我在学生表中将其作为外键引用时出错了吗?

学生ID上引用的
[列(顺序=3)]
是什么?@Eris。它只是一个普通属性和一个主键。
public class University
{
    [Key]
    public string Uni_ID { get; set; }
    public virtual List<Course> Courses { get; set; } 
}

 public class Course
{
    [Key]
    [Column(Order = 1)]
    public string Course_ID { get; set; }


    [Key,ForeignKey("uni")]
    [Column(Order = 2)]
    public string Uni_ID { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    public virtual University uni { get; set; }
}

    public class Student
{
    [Key,ForeignKey("course"), Column(Order = 1)]
    public string Course_ID { get; set; }

    [ForeignKey("course"),Column(Order = 2)]
    public string Uni_ID { get; set; }

    [Key]
    [Column(Order = 3)]
    public string Student_ID { get; set; }

    public virtual Course course { get; set; }
}