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
C# 使用实体框架6.1和MVC5为自引用创建CRUD_C#_Entity Framework_Asp.net Mvc 5 - Fatal编程技术网

C# 使用实体框架6.1和MVC5为自引用创建CRUD

C# 使用实体框架6.1和MVC5为自引用创建CRUD,c#,entity-framework,asp.net-mvc-5,C#,Entity Framework,Asp.net Mvc 5,我不熟悉实体框架,但我正在使用EF 6.1和MVC5,并试图构建一个与自身相关的课程实体(一个课程有一对一或一对多的预请求课程)。如何使用EF和CRUD功能实现这一点 我的模型如下:物理模型有一个与课程匹配的课程表和一个prereq表,该表有3列:prereqId,courseNo,preReqCourseNo public class Course { public int CourseId { get; set; } public string CourseNumber {

我不熟悉实体框架,但我正在使用EF 6.1和MVC5,并试图构建一个与自身相关的
课程
实体(一个
课程
有一对一或一对多的预请求课程)。如何使用EF和CRUD功能实现这一点

我的模型如下:物理模型有一个与
课程
匹配的
课程
表和一个
prereq
表,该表有3列:
prereqId
courseNo
preReqCourseNo

public class Course
{
    public int CourseId { get; set; }
    public string CourseNumber { get; set; }
    public string CourseName { get; set; }
    public decimal Credits { get; set; }
    public bool? PreReq { get; set; }
    public virtual ICollection<DeptCourse> DeptCourse  { get; set; }
    public virtual ICollection<Course> PreReq { get; set; }
}
公共课
{
public int CourseId{get;set;}
公共字符串枚举器{get;set;}
公共字符串CourseName{get;set;}
公共十进制积分{get;set;}
公共bool?PreReq{get;set;}
公共虚拟ICollection DeptCourse{get;set;}
公共虚拟ICollection PreReq{get;set;}
}

目标是能够在创建页面中定义这些属性,这样用户就可以从下拉列表中选择课程编号或倍数,并使用它们之间的关系更新这两个表。一门课程可以有一个或多个先修课程。多谢各位

正如Basic所说,我认为这确实是一种多对多的关系。但如果它真的是一对多,你可以这样制作你的模型:

 public class Course
    {
        public int CourseId { get; set; }
        public string CourseNumber { get; set; }
        public string CourseName { get; set; }
        public decimal Credits { get; set; }
        public bool? PreReq { get; set; }
        public int DepartmentID { get; set; }

        public int? PreRequisiteForCourseId {get; set;}
        public virtual Course PreRequisiteForCourse {get; set;}
        [InverseProperty("PreRequisiteForCourse")]
        public virtual ICollection<Course> PreReq { get; set; }
    }
公共课
{
public int CourseId{get;set;}
公共字符串枚举器{get;set;}
公共字符串CourseName{get;set;}
公共十进制积分{get;set;}
公共bool?PreReq{get;set;}
public int DepartmentID{get;set;}
public int?PreRequisiteForCourseId{get;set;}
公共虚拟课程的先决条件课程{get;set;}
[反向财产(“先决条件”)]
公共虚拟ICollection PreReq{get;set;}
}

在这种情况下,每门课程只能是一门课程的先决条件。

我已经做了一段时间了,但不是很多(例如,多门课程可能是多个不同课程的要求?)在任何情况下,您都可能希望添加“IsPreReqFor”互惠属性,以便可以在任意方向导航。毫无疑问,有人会简短地告诉你外键的语法。我确实有一个预请求复选框的bool,如果用户选择了它,我会提供一个带有可供选择的课程下拉列表的局部视图。我的意思是另一种方式。。。假设课程X需要A、B、C,而课程Y需要A、C、F。当看到课程C时,能够说“这门课程对X和Y都是一项要求”可能会很好。不需要额外的数据,只需要在任何方向上导航“需求”约束的能力。到底是什么问题?您是否正在寻求有关CRUD部件编码的帮助?如果是这样,请出示您的现有代码。对不起,您不在值班。现在的要求是能够添加新课程,并在此过程中能够定义一个或多个先决课程,例如,我正在创建课程501,我想定义该课程400,可能课程405是该课程的先决条件,因此当学生注册附加到课程501的课程时,系统将注册前,检查他是否参加了其他课程。