1.学校

[Table("School")]
public partial class School
{
public School()
{
Students = new List<Student>();
Teachers = new List<Teacher>();
}
[Key]
public string SchoolId { get; set; } public string Name { get; set; } public virtual ICollection<Student> Students { get; set; } public virtual ICollection<Teacher> Teachers { get; set; }
}

2.学生

[Table("Student")]
public partial class Student
{
public Student()
{
Teachers = new List<Teacher>();
}
[Key]
public string StudentId { get; set; } public string Name { get; set; } public string PhoneId { get; set; } public string SchoolId { get; set; } public virtual School School { get; set; } public ICollection<Teacher> Teachers { get; set; }
}

3.教师

[Table("Teacher")]
public class Teacher
{
public Teacher()
{
Students = new List<Student>();
}
[Key]
public string TeacherId { get; set; } public string Name { get; set; } public string PhoneId { get; set; } public string SchoolId { get; set; } public virtual School School { get; set; } public virtual ICollection<Student> Students { get; set; }
}

上面三个数据表映射成的实体类,关系是学校有多个学生和老师,每个学生和老师互相对应多个

关系配置在DbContext的OnModelCreating方法中配置

一对多

modelBuilder.Entity<Student>()
.HasRequired(a => a.School)
.WithMany(a => a.Students)
.HasForeignKey(a=>a.StudentId);
modelBuilder.Entity<Teacher>()
.HasRequired(a => a.School)
.WithMany(a => a.Teachers)
.HasForeignKey(a => a.TeacherId);

多对多

modelBuilder.Entity<Teacher>()
.HasMany(a => a.Students)
.WithMany(a => a.Teachers)
.Map
(
a =>
{
a.MapLeftKey("TeacherId");
a.MapRightKey("StudentId");
a.ToTable("Teacher_Student");
} );

最新文章

  1. 第三条:用私有构造器或者枚举类型强化Singleton属性
  2. php把错误日志输入到文件里。
  3. UIButton在不同状态下显示不同背景色
  4. hrbustoj 1161:Leyni(树状数组练习)
  5. js方法控制html表格的增加和删除
  6. 黑马程序员_&lt;&lt;IO流基本操作(Writer,Reader)&gt;&gt;
  7. maven添加oracle jdbc依赖
  8. windows命令行模式下无法打开python程序解决方法
  9. 迭代子模式(Iterator)
  10. com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector APPARENT DEADLOCK
  11. Python实现网站模拟登陆
  12. Django SNS 微博项目开发
  13. Mysql SQL分组取每组前几条记录
  14. P1233 木棍加工 dp LIS
  15. Linux(Centos7)下搭建SVN服务器
  16. 组队项目——黄金点(叶雨&amp;王浩)
  17. php-xdebug(安装)
  18. 【程序练习】——ini格式转换为xml格式
  19. day02(继承,重写,this,super,final,抽象类)
  20. 星空灯改装成USB供电

热门文章

  1. PlayJava Day002
  2. Java生鲜电商平台-SpringCloud微服务架构中核心要点和实现原理
  3. JS基础语法---数组基础知识总结
  4. kafka集群在消息消费出现无法找到topic分区的处理解决
  5. Angular 学习笔记(一)
  6. Event事件、进程池与线程池、协程
  7. CentOS7 安装Jenkins
  8. 在execCommand formatBlock &#39;p&#39;标签里增加class或id或css style?
  9. 8. Go语言—指针类型
  10. SpringCloud学习笔记(一、SpringCloud 基础)