我尝试了所有方法,但结果却完全相同。所有方法都相同吗?
我可以简单地添加第一个,为什么还要使用其他?
这是我看过的代码;
//1-One to Many
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Grade Grade { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
}
第二个是:
//second...
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public ICollection<Student> Students { get; set; }
}
第三个:
//third...
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Grade Grade { get; set; }
}
public class Grade
{
public int GradeID { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public ICollection<Student> Student { get; set; }
}
最后:
//fourth..
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int GradeId { get; set; }
public Grade Grade { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public ICollection<Student> Student { get; set; }
}
这里是我查看代码的链接:
答案 0 :(得分:0)
是的,你们都是对的,但是定义相同关系的方式不同。
您可以参考https://docs.microsoft.com/en-us/ef/core/modeling/relationships以获得更好的理解。