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 模式-建立多对多联系的更多相关文章

  1. Entity Framework Code First 模式-建立一对多联系

    一.建立一对多联系 使用的例子为Product与Category,一个种类(Product)对应多个商品(Product) 1.外键列名默认约定 在“一”这边的实体增加一个集合属性(public vi ...

  2. Entity Framework Code First 模式-建立一对一联系

    使用的例子为教室(ClassRoom),教室里的多媒体设备(Device),一个教室里有一套多媒体设备,一套多媒体设备只放在一个教室里. 1.Data Annotations方式 需要在任意一方的主键 ...

  3. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  4. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

  5. Entity Framework Code First 映射继承关系

    转载 http://www.th7.cn/Program/net/201301/122153.shtml Code First如何处理类之间的继承关系.Entity Framework Code Fi ...

  6. 使用 Entity Framework Code First

    使用 Entity Framework Code First 在家闲着也是闲着,继续写我的[ASP.NET MVC 小牛之路]系列吧.在该系列的上一篇博文中,在显示书本信息列表的时候,我们是在程序代码 ...

  7. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  8. Entity Framework Code First 学习日记(1)精

    我最近几天正在学习Entity Framework Code First.我打算分享一系列的学习笔记,今天是第一部分: 为什么要使用Code First: 近 年来,随着domain driven d ...

  9. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

随机推荐

  1. NX二次开发-UFUN打开信息窗口UF_UI_open_listing_window()

    NX9+VS2012 #include <uf.h> #include <uf_ui.h> UF_initialize(); //方法1(uc1601) uc1601();// ...

  2. NX二次开发-UFUN打开选择文件夹对话框UF_UI_create_filebox

    #include <uf.h> #include <uf_ui.h> #include <string> using namespace std; string O ...

  3. NX二次开发-UFUN工程图初始化视图信息UF_DRAW_initialize_view_info

    NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...

  4. NX二次开发-UFUN获取工程图视图边界线颜色UF_DRAW_ask_border_color

    #include <uf.h> #include <uf_draw.h> #include <uf_ui.h> UF_initialize(); ; UF_DRAW ...

  5. vuex存数据,防止刷新数据丢失

    1 created() { 2 if (sessionStorage.getItem('store')) { 3 this.$store.replaceState(Object.assign({}, ...

  6. openstack nova 源码解析 — Nova API 执行过程从(novaclient到Action)

    目录 目录 Nova API Nova API 的执行过程 novaclient 将 Commands 转换为标准的HTTP请求 PasteDeploy 将 HTTP 请求路由到具体的 WSGI Ap ...

  7. HTTP入门简介

    一.概念:Hyper Text Transfer Protocol 超文本传输协议 传输协议:定义了客户端和服务器端通信时,发送数据的格式 特点: 1.基于TCP/IP的高级协议 2.默认端口号:80 ...

  8. jsonArray转换成List

    从字符串String转换成List 字符串格式: String jsonstr = "{'studentsjson':[{'student':'张三'},{'student':'李四'}] ...

  9. 将sparkStreaming结果保存到Redshift数据库

    1.保存到redshift数据库的代码 package test05 import org.apache.log4j.{Level, Logger}import org.apache.spark.rd ...

  10. pytorch clamp 与clamp_区别

    pytorch clamp 与clamp_ ,有下划线的表示修改并付给自身,无下划线的表示需要返回处理后的值,比如: h = k.clamp(min=0) #将结果存入h,k保留原值 k.clamp_ ...