如何在使用实体框架code首先两个实体之间的多个一对许多关系 [英] How to have multiple one-to-many relations between two entities using Entity Framework Code First

查看:97
本文介绍了如何在使用实体框架code首先两个实体之间的多个一对许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个简单的方法来拯救这是工作完全正常的关系型数据库的记录。我怀疑一个场景。在此之前,我需要知道我正在接近方式有如果数据库复杂性的增加任何困难。任何更好的,有效的,但简单的方法?

Below is a simple approach to save relational database records which is working perfectly fine. I have doubt on one scenario. Before that i need to know the way i am approaching has any difficulties if the database complexity increases. Any better, efficient but simple approach?

一对一:

tb_student // store student details
id, name, country_id (country_id foriegnkey set with id of tb_country)

tb_country // store all available countries
id, name

[Table("tb_student")]
public class Student
{    
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    public Country country { get; set; }
}
[Table("tb_country")]
public class Country
{    
    [Key]
    public int id { get; set; }
    public string name { get; set; }
} 

student come as parameter or create new student
Country _country = // we have selected country

StudentModelContext sdb = new StudentModelContext();
student.country = _country;
sdb.students.Add(student);
sdb.SaveChanges();

一对多:

tb_student // store student details
id, name

tb_typesubject // store all available subjects
id, name

tb_subject // store student - subject relation
id, student_id, subjecttypeid

[Table("tb_student")]
public class Student
{    
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    public List<Subject> subjects { get; set; }
} 
[Table("tb_typesubject")]
public class TypeSubject
{    
    [Key]
    public int id { get; set; }
    public string name { get; set; }
} 
[Table("tb_subject")]
public class Subject
{    
    [Key]
    public int id { get; set; }
    public int subjecttypeid { get; set; }
    // we dont have to create student_id here
} 

student come as parameter or create new student
TypeSubject _subjType1 = // we have selected subject list
TypeSubject _subjType2 = // we have selected subject list

Subject _subject1 = new Subject();
_subject1.subjecttypeid = _subjType1.id;
Subject _subject2 = new Subject();
_subject2.subjecttypeid = _subjType2.id;

StudentModelContext sdb = new StudentModelContext();
student.subjects = new List<Subject>;
student.subjects.add(_subject1);
student.subjects.add(_subject2);
sdb.students.Add(student);
sdb.SaveChanges();

这完美的作品。我非常高兴。我们可以通过

This works perfectly. And i am very glad. We can load all values by

Student stud = sd.students.Find(1);
stud.Entry(stud).Collection(s => s.subjects).Load();

如果学生能给予分期付款的费用为每个主题

If student can give fees by installment for each subject

for (int i = 0; i < stud.subjects.Count; i++)
    sd.Entry(stud.subjects[i]).Collection(f => f.fees).Load();

我的疑问是如何设计下列之情况:

My doubt is how to design following scenerio:

有将审查是由另一名学生送每一个学生。如何像类做到这一点:

There will be review for each student which is send by another student. How to do this for class like:

[Table("tb_student")]
public class Student
{    
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    public List<Review> reviews { get; set; }
} 
[Table("tb_review")]
public class Review
{    
    [Key]
    public int id { get; set; }
    public string message { get; set; }
    public int student_id { get; set; } // review of which student
    public Student reviewer { get; set; } // whom send the review
} 

任何帮助吗?

推荐答案

尝试添加2学生您的评论类,例如:

Try to add 2 Students in your Review class, for example:

[Table("tb_review")]
public class Review
{    
    [Key]
    public int id { get; set; }
    public string message { get; set; }
    public Student student{ get; set; } // review of which student
    public Student reviewer{ get; set; } // whom send the review
} 

和你的学生类应该是这样的:

And your Student class should be like this:

[Table("tb_student")]
public class Student
{    
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    [InverseProperty("student")]
    public List<Review> reviewAbout{ get; set; }
    [InverseProperty("reviewer")]
    public List<Review> reviewBy{ get; set; }
} 

这篇关于如何在使用实体框架code首先两个实体之间的多个一对许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆