Configure Many-to-Many relationship:
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:
Configure Many-to-Many relationship:的更多相关文章
- 国产深度学习框架mindspore-1.3.0 gpu版本无法进行源码编译
官网地址: https://www.mindspore.cn/install 所有依赖环境 进行sudo make install 安装,最终报错: 错误记录信息: cat /tmp/mind ...
- 9.Configure One-to-One(配置一对一关系)【Code-First系列】
现在,开始学习怎么配置一对一的关系,众所周知,一对一的关系是:一个表中的主键,在另外一个表中,同时是主键和外键[实际上是一对零或者一对一]. 请注意:一对一的关系,在MS SQL Server中,技术 ...
- 10.Configure One-to-Many(配置一对多关系)【Code-First系列】
现在,我们将学习怎么配置一对多的关系. Visit Entity Relationship section to understand how EF manages one-to-one, one-t ...
- How to Configure the Gradient Boosting Algorithm
How to Configure the Gradient Boosting Algorithm by Jason Brownlee on September 12, 2016 in XGBoost ...
- "No appenders found for logger" and "Please configure log4j properly"
Why do I see a warning about "No appenders found for logger" and "Please configure lo ...
- 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 ...
- Entity Framework Code-First(12):Configure One-to-Many
Configure One-to-Many Relationship: Here, we will learn how to configure One-to-Many relationship be ...
- Entity Framework Code-First(11):Configure One-to-One
Configure One-to-Zero-or-One Relationship: Here, we will configure One-to-Zero-or-One relationship b ...
- Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7
SOLUTION VERIFIED September 13 2016 KB1248793 Environment Red Hat Enterprise Linux 7 NetworkManager ...
随机推荐
- 关于requests库中文编码问题
转自:代码分析Python requests库中文编码问题 Python reqeusts在作为代理爬虫节点抓取不同字符集网站时遇到的一些问题总结. 简单说就是中文乱码的问题. 如果单纯的抓取微博 ...
- linux就该这么学,第十一天了
今天讲了,网卡绑,定,两块网卡同时工作,自动备源,理论上速度提升一倍,工作中可以用到的技术 还有sshd服务,端口22,远程连接使用,还可以设置root是否可以直接登录,主要配置文件在,/etc/ss ...
- caoni大业 spring boot 跳坑记
IDEA环境 win10 跑得刚刚,到xp系统就戈壁 报错 Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.g ...
- qhfl-6 购物车
购物车中心 用户点击价格策略加入购物车,个人中心可以查看自己所有购物车中数据 在购物车中可以删除课程,还可以更新购物车中课程的价格策略 所以接口应该有四种请求方式, get,post,patch,de ...
- Maven二
1 回顾 1.1 Maven的好处 节省空间 对jar包做了统一管理 依赖管理 一键构建 可跨平台 应用在大型项目可提高开发效率 1.2 Maven安装部署配置 1.3 Maven的仓库 本地仓库 远 ...
- Oracle存储过程,游标使用
Oracle存储过程: 语法: CREATE [OR REPLACE] PROCEDURE procedure_name (arg1 [mode1] datatype1,arg2 [mode2] da ...
- Java变成思想总结
Java容器 collection一个独立的元素序列,这些元素都服从一条或多条规则.list必须按照插入的顺序保存元素,set不能有重复元素,queue按照排队的顺序确定对象产生的顺序.map一组成对 ...
- Oracle数据库查询基本数据
------------------------------------------------------------------找出EMP表select * from EMP;--选择在部门30中 ...
- java基础-位运算符
1.位运算符 << 左移 : 右边以0填充 >> 带符号右移: 负数前面补1,整数补0 >>>不带符号右移 & 按位与运算 ...
- Mybatis延迟加载、缓存
一.Mybatis中的延迟加载 1.延迟加载背景:Mybatis中Mapper配置文件中的resultMap可以实现高级映射(使用association.collection实现一对一及一对多(多对多 ...