【发布时间】:2016-05-12 01:00:48
【问题描述】:
我正在使用 Entity Framework 7 RC1,我实际上是在尝试创建一个字段,以便我可以保存学生标准(一个学生可以有多个标准,每个标准可以属于多个学生)。按照 asp.net 文档,我设置了表格,并使用一些组成的数据在 SQL Server 中手动填写了 Student 表、Standard 表和 StudentStandard 表。但是,当我在运行时调试代码/视图时,当我在控制器中执行 getAll 时,我发现 StudentStandards 字段为“null”。我正在使用以下代码访问视图中的此字段:model.Students.StudentStandards.Select(c=>c.StandardId) 但这没有提供任何内容。
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; }
public ICollection<StudentStandard> StudentStandards { get; set; }
}
public class StudentStandard
{
[ForeignKey("Student")]
public int StudentId { get; set; }
public Student Student { get; set; }
[ForeignKey("Standard")]
public int StandardId { get; set; }
public Standard Standard { get; set; }
}
public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; }
public ICollection<StudentStandard> StudentStandards { get; set; }
}
这是使用迁移创建表后的样子:
我的问题:我如何获得这种 m:m 关系来存储和检索这些数据?
【问题讨论】:
标签: entity-framework asp.net-core entity-framework-core