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) 简单实体映 ...
随机推荐
- find查找文件
linux下最强大的搜索命令为”find“. 它的格式为”find <指定目录> <指定条件> <指定动作>“: 比如使用find命令搜索在根目录下的所有inter ...
- CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境, 记坑篇
CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境 CentOS7 + Python3 + D ...
- nginx location 语法
location 语法location 有”定位”的意思, 根据Uri来进行不同的定位.在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.比如, 碰到.p ...
- ffmpeg 获取视频宽高
int main(int argc, char *argv[]) { const char* file_name = "video.mp4"; int ret; unsigned ...
- Codeforces 351B Jeff and Furik:概率 + 逆序对【结论题 or dp】
题目链接:http://codeforces.com/problemset/problem/351/B 题意: 给你一个1到n的排列a[i]. Jeff和Furik轮流操作,Jeff先手. Jeff每 ...
- 分享知识-快乐自己:Struts2 拦截器 与 过滤器
拦截器的使用以及配置: package com.gdbd.interceptor; import com.gdbd.pojo.UserInfo; import com.opensymphony.xwo ...
- windows 里面waveOut*接口应用
#include <windows.h>#include <mmsystem.h>#include <stdio.h>/** some good values fo ...
- 计算机丨浏览器访问出现DNS_PROBE_POSSIBLE解决方法
方法1.打开命令控制台输入: netsh winsock reset.提示重启,电脑重启后就ok了. 其他方法待续......
- 【leetcode刷题笔记】Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- ffmpeg avpicture_fill的一些使用
标签: ffmpegavpicture_fill 2013-05-17 10:03 4713人阅读 评论(1) 收藏 举报 分类: ffmpeg(3) 这个FFMPEG我没找到详细的中文教程,只有 ...