如何使用Entity Framework Code First在两个实体之间建立多个一对多关系

时间:2014-08-18 14:09:57

标签: entity-framework asp.net-mvc-4 relational-database code-first

以下是一种保存关系数据库记录的简单方法,它可以正常工作。我对一个场景有疑问。在此之前,如果数据库复杂性增加,我需要知道我接近的方式有任何困难。任何更好,更有效但更简单的方法?

ONE to ONE:

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();

ONE to MANY:

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();

这完美无缺。我很高兴。我们可以通过

加载所有值
Student stud = sd.students.Find(1);
stud.Entry(stud).Collection(s => s.subjects).Load();

如果学生可以分期为每个科目分期付费

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

我怀疑如何设计以下场景:

将由另一名学生发送的每位学生进行复习。如何为类这样做:

[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 个答案:

答案 0 :(得分:2)

尝试在Review课程中添加2名学生,例如:

[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
} 

你的学生班应该是这样的:

[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; }
} 

答案 1 :(得分:1)

namespace MvcApplication4.Models
{
 [Table("tb_book")]
public class Book
{
    [Key]
    public int ID { get; set; }
    public string Title { get; set; }

    [InverseProperty("Books")]
    public Author Author { get; set; }
}
[Table("tb_author")]
public class Author
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    [InverseProperty("Author")]
    public ICollection<Book> Books { get; set; }
}
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class StudentModelContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
}
}

表格结构


CREATE TABLE `tb_book` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(45) DEFAULT NULL,
`Author_ID` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `ab_idx` (`Author_ID`),
CONSTRAINT `ab` FOREIGN KEY (`Author_ID`) REFERENCES `tb_author` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;


CREATE TABLE `tb_author` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;