Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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#_Asp.net_Entity Framework_Ef Code First_Code First - Fatal编程技术网

C# 实体框架代码通过关系表的第一个导航属性

C# 实体框架代码通过关系表的第一个导航属性,c#,asp.net,entity-framework,ef-code-first,code-first,C#,Asp.net,Entity Framework,Ef Code First,Code First,我正在尝试使用一些实体框架代码优先模型设置一些导航属性。我希望它们看起来像这个例子: public class Course { [Key] public int CourseId { get; set; } public string CourseName { get; set; } public virtual ICollection<Student> Students { get; set; } } public class Student {

我正在尝试使用一些实体框架代码优先模型设置一些导航属性。我希望它们看起来像这个例子:

public class Course
{
    [Key]
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

public class Student
{
    [Key]
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
}

public class StudentCourses
{
    [Key, Column(Order = 0)]
    public int StudentId { get; set; }
    public virtual Student Student { get; set; }
    [Key, Column(Order = 1)]
    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
}
公共课
{
[关键]
public int CourseId{get;set;}
公共字符串CourseName{get;set;}
公共虚拟ICollection学生{get;set;}
}
公立班学生
{
[关键]
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
公共虚拟ICollection课程{get;set;}
}
公共班级学生课程
{
[键,列(顺序=0)]
公共int StudentId{get;set;}
公共虚拟学生学生{get;set;}
[键,列(顺序=1)]
public int CourseId{get;set;}
公共虚拟课程{get;set;}
}

因此,学生和课程的关系将在StudentCourses表中建立。学生类的实例将自动引用该学生的所有课程,反之亦然,课程类的实例将自动引用其所有学生。StudentCourses类的实例将自动引用其学生和课程。但当我试图更新数据库时,这些关系似乎没有得到正确的解释。这里有我遗漏的东西吗?也许需要在上下文类中进行一些配置?大多数导航属性示例仅显示一对多关系导航属性。

当您有
M:M
关系时,您需要构建如下所示的模型。您不需要构造连接表。EF将在您执行数据迁移时为您创建一个

使用约定的模型配置。

public class Student
{
    public Student() 
    {
        this.Courses = new HashSet<Course>();
    }

    public int StudentId { get; set; }

    public string StudentName { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}

public class Course
{
    public Course()
    {
        this.Students = new HashSet<Student>();
    }

    public int CourseId { get; set; }
    public string CourseName { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}
公共班级学生
{
公立学生()
{
this.Courses=newhashset();
}
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
公共虚拟ICollection课程{get;set;}
}
公共课
{
公共课程()
{
this.Students=newhashset();
}
public int CourseId{get;set;}
公共字符串CourseName{get;set;}
公共虚拟ICollection学生{get;set;}
}
您的上下文类应该是这样的

public class YourDBContext : DBContext
{
    public YourDBContext () : base("Default")
    {
    }

    public DbSet<Student> Students { get; set; }

    public DbSet<Course> Courses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}
public类YourDBContext:DBContext
{
public YourDBContext():base(“默认值”)
{
}
公共数据库集学生{get;set;}
公共数据库集课程{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
}
}

在遵循Sampath的建议后,我实际上决定将一些其他属性附加到关系中。因此,我最终定义了StudentCourses类,如下所示:

public class StudentCourses
{
    [Key, Column(Order = 0)]
    public int StudentId { get; set; }
    public virtual Student Student { get; set; }
    [Key, Column(Order = 1)]
    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
    public int Grade { get; set; }
}
public class Course
{
    [Key]
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public virtual ICollection<StudentCourses> Students { get; set; }
}

public class Student
{
    [Key]
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual ICollection<StudentCourses> Courses { get; set; }
}
所以我改变了学生和课程如下:

public class StudentCourses
{
    [Key, Column(Order = 0)]
    public int StudentId { get; set; }
    public virtual Student Student { get; set; }
    [Key, Column(Order = 1)]
    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
    public int Grade { get; set; }
}
public class Course
{
    [Key]
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public virtual ICollection<StudentCourses> Students { get; set; }
}

public class Student
{
    [Key]
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual ICollection<StudentCourses> Courses { get; set; }
}
公共课
{
[关键]
public int CourseId{get;set;}
公共字符串CourseName{get;set;}
公共虚拟ICollection学生{get;set;}
}
公立班学生
{
[关键]
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
公共虚拟ICollection课程{get;set;}
}

最重要的是,我没有将StudentCourses添加到DbContext中。因此,在执行数据库更新后,EF自动为StudentCourses创建了表,导航属性都起作用。

Ah,所以EF将该表从模型中完全抽象出来。我原以为我需要为EF创建的连接表创建一个类,但我认为导航属性不需要这样做。是的,EF将为您创建一个连接表。您不需要手动创建。