Entity Framework Code-First(13):Configure Many-to-Many
Configure Many-to-Many relationship:
Here, we will learn how to configure Many-to-Many relationship between the Student and Course entity classes. Student can join multiple courses and multiple students can join one course.
Visit Entity Relationship section to understand how EF manages one-to-one, one-to-many and many-to-many relationships between the entities.
Configure Many-to-Many relationship using DataAnnotation:
Student class should have a collection navigation property for Course, and Course should have a collection navigation property for student, which will create a Many-to-Many relationship between student and course as shown below:
- public class Student
- {
- public Student() { }
- public int StudentId { get; set; }
- [Required]
- public string StudentName { get; set; }
- public int StdandardId { get; set; }
- public virtual ICollection<Course> Courses { get; set; }
- }
- public class Course
- {
- public Course()
- {
- this.Students = new HashSet<Student>();
- }
- public int CourseId { get; set; }
- public string CourseName { get; set; }
- public virtual ICollection<Student> Students { get; set; }
- }
The code shown above will create the following database, where Code-First will create a third joining table, CourseStudent, which will consist of the PK of both the tables, i.e. StudentId & CourseId:
Configure Many-to-Many relationship using Fluent API:
You can use the Fluent API to configure a Many-to-Many relationship between Student and Course, as shown below:
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Student>()
- .HasMany<Course>(s => s.Courses)
- .WithMany(c => c.Students)
- .Map(cs =>
- {
- cs.MapLeftKey("StudentRefId");
- cs.MapRightKey("CourseRefId");
- cs.ToTable("StudentCourse");
- });
- }
As you can see in the above example, .HasMany<Course>(s => s.Courses).WithMany(c => c.Students)
says that Student and Course has many-to-many relationship with Students navigation property in Course class and Courses navigation property in Student class.
Map method takes Action type delegate, hence, we can pass lambda expression wherein we will specify FK property name of Student (we start with Student entity, so it will be left table) and FK of Course table. ToTable will create StudentCourse table.
This will create a new joining table StudentCourse with two Primary Keys which will also be Foreign Keys, as shown below:
Entity Framework Code-First(13):Configure Many-to-Many的更多相关文章
- Entity Framework Tutorial Basics(13):Database First
Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- Entity Framework Code First (八)迁移 Migrations
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...
- Entity Framework Code First (六)存储过程
声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映 ...
随机推荐
- 基于msm8909高通平台Android驱动开发之hello程序
本文转载自:http://www.itwendao.com/article/detail/227839.html Android驱动开发之Hello实例: 驱动部分 modified: ker ...
- POJ 之 Hardwood Species
Hardwood Species Time Limit:10000MS Memory ...
- Git 远程仓库 git remote
http://blog.csdn.net/s0228g0228/article/details/45368155 Git remote -v 查看现有远程仓库的地址url 三种方式都可以. 1. 修改 ...
- 算法(Algorithms)第4版 练习 2.1.1
E A S Y Q U E S T I O N A E S Y Q U E S T I O N A E S Y Q U E S T I O N A E E Y Q U S S T I O N A E ...
- SQL truncate 、delete与drop区别及 MSSQL、MySQL 数据库删除大批量千万级百万级数据的优化
C#_Stopwatch 类 http://www.cnblogs.com/zhw511006/archive/2009/07/22/1528405.html http://blog.csdn.net ...
- Thriftpy一个简单的例子
sleep.thrift文件(什么是thrift文件?),文件内容如下,该文件定义了一个Sleep服务,该服务提供一个sleep方法,sleep方法接受一个32位int类型的参数且没有返回值 serv ...
- linux lvm
一.linux的lv(logical volume) lv各层次示例图如下: 核心思想:最底层的pv就是一个一个的磁盘,在保证总体容量的情况下,可以移除部分磁盘,在pv上面设置一个vg,相当于vg把所 ...
- Tab支持的DHTML Window控件
带有Tab标签支持的DHTML Window控件.它使用cookies来“记忆”窗体大小,位置,哪个Tab选项被选中,window堆叠顺序.代码下载地址:http://www.huiyi8.com/ ...
- 英语发音规则---/ŋ/与/ŋg/的读音区别
英语发音规则---/ŋ/与/ŋg/的读音区别 一.总结 一句话总结: 1.位于词中间的字母组合ng,有时读作/ ŋ /,有时读作/ ŋg/? singer ['sɪŋə] n. 歌手 ringing ...
- eclipse导入jar包连接mysql
Eclipse中导入 mysql--conncetor --java--jars 方法一:在工程项上右击,点Build Path->Configure Build Path-->Libr ...