11.Configure Many-to-Many(配置多对多关系)【Code-First系列】
2024-08-30 04:38:39
现在学习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); } } }
好了这就是多对多的关系的配置了。
附上目录:
- 什么是Code First
- 简单的Code First例子
- Code-First 约定
- DB Initialization(数据库初始化)
- Inheritance Strategy(继承策略)
- Configure Domain Classes(配置领域类)
- DataAnnotations(数据注解)
- Fluent API
- Configure One-to-One(配置一对一关系)
- Configure One-to-Many(配置一对多关系)
- Configure Many-to-Many(配置多对多关系)
- Move Configurations(数据迁移)
- DB Initialization Strategy(数据库初始化策略)
最新文章
- DOM扩展-HTML5、专有扩展
- React Native微信分享 朋友圈分享 Android/iOS 通用
- 设置ASP.NET MVC站点默认页为html页
- Magento速度优化
- 斌哥的 Docker 进阶指南—监控方案的实现
- JavaScript学习笔记 - 进阶篇(1)- JS基础语法
- IOS_设置启动图片若干问题
- bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班
- HDU_2056——相交矩形的面积
- C#的Socket编程
- Spring Boot实战之逐行释义HelloWorld
- NGUI_Texture
- 数据挖掘进阶之序列模式分析算法GSP的实现
- mysql5.7连接不上可能的问题(针对新安装的mysql5.7可能出现的问题)
- 如何学习SpringCloud?(SpringCloud模板)
- dataframe基础
- IDM 破解
- echarts柱状图标签显示不完全的问题
- Linux并发执行很简单,这么干就对了
- requirejs案例
热门文章
- 使用 MimeKit 和 MailKit 发送邮件
- 飞流直下的精彩 -- 淘宝UWP中瀑布流列表的实现
- Three.js + HTML5 Audio API 打造3D音乐频谱,Let’s ROCK!
- 几个常用Json组件的性能测试
- My first makefile to compile multiple C files
- windows命令——taskmgr 1
- Docker学习笔记
- 替代jquery1.9版本以前的toggle事件函数(开关)
- UpdateData(TRUE)与UpdateData(FALSE)的使用
- php的mysql\mysqli\PDO(三)PDO