Entity Framework Code First 模式-建立多对多联系
Entity Framework 在建立多对多的联系时,会生成一个中间表,用来表示这个多对多的关系。这和数据库设计时从概念模型到逻辑模型转化时,多对多的关系不能和任何一端的实体合并,需要将关系也转化为关系模型。例子使用角色(Role)和用户(User),一个角色会有多个用户,一个用户拥有多个角色。
1.默认约定
代码:
public partial class Role
{
public int RoleID { get; set; }
public string RoleName { get; set; } public virtual ICollection<User> Users { get; set; }
}
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<bool> IsValid { get; set; } public virtual ICollection<Role> Roles { get; set; }
}
结果:



2.FluentAPI 方式
这边展示用映射类的方式去建立表,即为每个实体类去建立一个映射到数据库的类,在这里面定义映射到数据库的相关属性。
代码:
public partial class Role
{
public int RoleID { get; set; }
public string RoleName { get; set; } public virtual ICollection<User> Users { get; set; }
}
Role类
public class RoleMap:EntityTypeConfiguration<Role>
{
public RoleMap()
{
//主键
this.HasKey(s => s.RoleID); //属性的特性
this.Property(s => s.RoleName)
.HasMaxLength(); //类映射到数据库表和列的相关说明
this.ToTable("Role");
this.Property(s => s.RoleID).HasColumnName("Id"); //实体关系之间的定义
this.HasMany(s => s.Users)
.WithMany(s => s.Roles)
.Map(m =>
{
m.ToTable("RoleUser");
m.MapLeftKey("RoleId");
m.MapRightKey("UserID");
}); }
}
RoleMap 映射类
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Nullable<bool> IsValid { get; set; } public virtual ICollection<Role> Roles { get; set; }
}
User 类
public UserMap()
{
//主键
this.HasKey(s => s.UserID); //属性
this.Property(s => s.UserName)
.HasMaxLength(); // //类映射到数据库表和列的相关说明
this.ToTable("User");
this.Property(s => s.UserName).HasColumnName("Name");
}
UserMap 映射类
在数据上下文类中的OnModelCreating方法加入如下代码:
modelBuilder.Configurations.Add(new RoleMap());
modelBuilder.Configurations.Add(new UserMap());
结果:



Entity Framework Code First 模式-建立多对多联系的更多相关文章
- Entity Framework Code First 模式-建立一对多联系
一.建立一对多联系 使用的例子为Product与Category,一个种类(Product)对应多个商品(Product) 1.外键列名默认约定 在“一”这边的实体增加一个集合属性(public vi ...
- Entity Framework Code First 模式-建立一对一联系
使用的例子为教室(ClassRoom),教室里的多媒体设备(Device),一个教室里有一套多媒体设备,一套多媒体设备只放在一个教室里. 1.Data Annotations方式 需要在任意一方的主键 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First 映射继承关系
转载 http://www.th7.cn/Program/net/201301/122153.shtml Code First如何处理类之间的继承关系.Entity Framework Code Fi ...
- 使用 Entity Framework Code First
使用 Entity Framework Code First 在家闲着也是闲着,继续写我的[ASP.NET MVC 小牛之路]系列吧.在该系列的上一篇博文中,在显示书本信息列表的时候,我们是在程序代码 ...
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First 学习日记(1)精
我最近几天正在学习Entity Framework Code First.我打算分享一系列的学习笔记,今天是第一部分: 为什么要使用Code First: 近 年来,随着domain driven d ...
- Entity Framework Code First学习系列目录
Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...
随机推荐
- luoguP4768 [NOI2018]归程
传送门 kruskal重构树: kruskal合并两个联通块时合并的边一定是联通块中权值最大的边,小于等于这条边的边所能联通的所有点在这个联通块中. 在合并两个联通块的时候新建一个点作为两个联通块代表 ...
- C++从string中删除所有的某个特定字符【转载】
转载自https://www.cnblogs.com/7z7chn/p/6341453.html C++中要从string中删除所有某个特定字符, 可用如下代码 str.erase(std::remo ...
- 2018 – 2019 年前端 JavaScript 面试题
JavaScript 基础问题 1.使以下代码正常运行: JavaScript 代码: const a = [1, 2, 3, 4, 5]; // Implement this a.multiply( ...
- ionic-CSS:ionic 单选框
ylbtech-ionic-CSS:ionic 单选框 1.返回顶部 1. ionic 单选框 ionic 单选按钮与标准 type="radio" 的 input元素类似.以下展 ...
- LeetCode 620. Not Boring Movies (有趣的电影)
题目标签: 题目给了我们一个 cinema 表格, 让我们找出 不无聊的电影,并且id 是奇数的,降序排列. 比较直接简单的,具体看code. Java Solution: Runtime: 149 ...
- CodeForces-1244C-The Football Season-思维
The football season has just ended in Berland. According to the rules of Berland football, each matc ...
- CTF里的LSB
- Ubuntu下安装fcitx+搜狗输入法
转载自:http://www.linuxidc.com/Linux/2013-07/87062.htm 目标:在Ubuntu 13.04以及基于Ubuntu的发行版上安装fcitx小企鹅输入法,并安装 ...
- 读取.properties配置文件(转载)
读取.properties 文件 配置文件的一种,内容以键值对的形式存在,且每个键值对独占一行.#号作为行注释的起始标志,中文注释会自动进行unicode编码.示例: # ip and port of ...
- 网页添加Live2D看板娘
看板娘简而言之就是小店的女服务生,也有“吸引顾客,招揽生意,提高人气”等作用类似品牌形象代言人的含义. 如果想放一个呆萌的看板娘在博客上 js <script type="text/j ...