现在学习EF Code-First多对多的配置。

这里我们举例:学生和班级实体,一个学生可以选修多个课程,多个学生也可以选修同一个课程。

一、使用数据注解特性,配置多对多的关系

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF7
{
   public class Student
    {
       public int StudentId { get; set; }

       public string StudentName { get; set; }

       public virtual ICollection<Course> Courses { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EF7
{
   public class Course
    {
       public int CourseID { get; set; }

       public string CourseName { get; set; }

       public ICollection<Student> Students { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF7
{
   public class DbContextClass:DbContext
    {
       public DbContextClass()
           : base("ConnectionStrings")
       {
           Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>());
       }

       public DbSet<Student> Students { get; set; }

       public DbSet<Course> Courses { get; set; }
    }
}

然后生成的数据库:

上面的代码,是Code-First默认约定,帮我们自动配置的多对多关系。,可以看到生成了一个中间表StudentCourse。

二、现在让我们来使用Fluent API来配置多对多的关系吧:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF7
{
   public class DbContextClass:DbContext
    {
       public DbContextClass()
           : base("ConnectionStrings")
       {
           Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>());
       }

       public DbSet<Student> Students { get; set; }

       public DbSet<Course> Courses { get; set; }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Entity<Student>().HasMany(s => s.Courses).WithMany(s => s.Students).Map(m =>
           {
               m.MapLeftKey("CourseRefID");
               m.MapRightKey("StudentRefID");
               m.ToTable("哇哈哈哈", "xxx");
           });
           base.OnModelCreating(modelBuilder);
       }
    }
}

好了这就是多对多的关系的配置了。

附上目录:

最新文章

  1. DOM扩展-HTML5、专有扩展
  2. React Native微信分享 朋友圈分享 Android/iOS 通用
  3. 设置ASP.NET MVC站点默认页为html页
  4. Magento速度优化
  5. 斌哥的 Docker 进阶指南—监控方案的实现
  6. JavaScript学习笔记 - 进阶篇(1)- JS基础语法
  7. IOS_设置启动图片若干问题
  8. bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班
  9. HDU_2056——相交矩形的面积
  10. C#的Socket编程
  11. Spring Boot实战之逐行释义HelloWorld
  12. NGUI_Texture
  13. 数据挖掘进阶之序列模式分析算法GSP的实现
  14. mysql5.7连接不上可能的问题(针对新安装的mysql5.7可能出现的问题)
  15. 如何学习SpringCloud?(SpringCloud模板)
  16. dataframe基础
  17. IDM 破解
  18. echarts柱状图标签显示不完全的问题
  19. Linux并发执行很简单,这么干就对了
  20. requirejs案例

热门文章

  1. 使用 MimeKit 和 MailKit 发送邮件
  2. 飞流直下的精彩 -- 淘宝UWP中瀑布流列表的实现
  3. Three.js + HTML5 Audio API 打造3D音乐频谱,Let’s ROCK!
  4. 几个常用Json组件的性能测试
  5. My first makefile to compile multiple C files
  6. windows命令——taskmgr 1
  7. Docker学习笔记
  8. 替代jquery1.9版本以前的toggle事件函数(开关)
  9. UpdateData(TRUE)与UpdateData(FALSE)的使用
  10. php的mysql\mysqli\PDO(三)PDO