博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
问题记录:EntityFramework 一对一关系映射
阅读量:6151 次
发布时间:2019-06-21

本文共 5081 字,大约阅读时间需要 16 分钟。

EntityFramework 一对一关系映射有很多种,比如主键作为关联,配置比较简单,示例代码:

public class Teacher{    public int Id { get; set; }    public string Name { get; set; }    public virtual Student Student { get; set; }}public class Student{    public int Id { get; set; }    public string Name { get; set; }    public virtual Teacher Teacher { get; set; }}

上面代码表示 Teacher 和 Student 一对一关系,Fluent API 配置如下:

modelBuilder.Entity
() .HasRequired(x => x.Student) .WithOptional(x => x.Teacher);modelBuilder.Entity
();

测试代码:

var teachers = await _teacherRepository.GetAll().Include(x => x.Student).ToListAsync()

生成 SQL 代码:

SELECT     [Extent1].[Id] AS [Id],     [Extent1].[Name] AS [Name],     [Extent2].[Id] AS [Id1],     [Extent2].[Name] AS [Name1]    FROM  [dbo].[Teachers] AS [Extent1]    INNER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]

另一种 Fluent API 配置如下:

modelBuilder.Entity
();modelBuilder.Entity
() .HasRequired(x => x.Teacher) .WithOptional(x => x.Student);

执行同样测试代码,生成 SQL 代码:

SELECT     [Extent1].[Id] AS [Id],     [Extent1].[Name] AS [Name],     [Extent2].[Id] AS [Id1],     [Extent2].[Name] AS [Name1]    FROM  [dbo].[Teachers] AS [Extent1]    LEFT INNER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]

根据上面的测试情况,我们可以得到一些信息,首先测试代码查询 Teacher,然后 Inclue Student,Fluent API 配置的不同,生成的 SQL 代码也不同:

  • Fluent API 配置 Teacher,HasRequired Student 对应 INNER JOIN
  • Fluent API 配置 Student,HasRequired Teacher 对应 LEFT INNER JOIN

我们可以得出,一对一关系,Fluent API 只需要配置一个实体就可以了,根据查询关联的不同,配置对应的 HasRequired 和 WithOptional。

一对一关系,除了两个实体主键映射外,还有一种情况就是主键和外键映射,可以理解为主表和子表映射,示例代码:

public class Teacher{    public int Id { get; set; }    public string Name { get; set; }}public class Student{    public int Id { get; set; }    public string Name { get; set; }    public int TeacherId { get; set; }    public virtual Teacher Teacher { get; set; }}

Student 中有一个 TeacherId 属性,对应 Teacher 中的主键 Id,在微软的官方示例中,Student 是作为主表,Teacher 作为子表,也就是说,我们在查询的时候是查询的 Student,然后 Include Teacher,Fluent API 配置:

modelBuilder.Entity
();modelBuilder.Entity
() .HasRequired(x => x.Teacher) .WithMany() .HasForeignKey(x => x.TeacherId);

测试代码:

var students = await _studentRepository.GetAll().Include(x => x.Teacher).ToListAsync();

生成 SQL 代码:

SELECT     [Extent1].[Id] AS [Id],     [Extent1].[Name] AS [Name],     [Extent1].[TeacherId] AS [TeacherId],     [Extent2].[Id] AS [Id1],     [Extent2].[Name] AS [Name1]    FROM  [dbo].[Students] AS [Extent1]    INNER JOIN [dbo].[Teachers] AS [Extent2] ON [Extent1].[TeacherId] = [Extent2].[Id]

这是没有什么问题的,需要注意的是 Teacher 中并没有 Student 的导航属性,如果直接添加的话,运行会直接报错(Teachers 表默认生成的 Student_Id 字段找不到),解决方式是需要配置 Teacher 的相关 Fluent API。

上面的一对一关系,其实就是主表的一个子表扩展,在主表中存储子表的主键作为外键,查询的时候直接 Include 子表就可以了,但还有一种情况是,我查询子表,然后 Include 主表,主表的主键存储在子表中作为外键,这里的主表和子表概念只是相对的。

比如上面场景中,我查询 Teacher 然后把 Student Include 包含进来,如果是上面的配置是没有办法的,因为 Teacher 并没有配置导航属性,所以,我们需要改一下代码:

public class Teacher{    public int Id { get; set; }    public string Name { get; set; }    public virtual Student Student { get; set; }}public class Student{    public int Id { get; set; }    public string Name { get; set; }    public int TeacherId { get; set; }    public virtual Teacher Teacher { get; set; }}

上面说过,Teacher 增加 Student 导航属性会直接报错,然后我们再修改下 Fluent API 配置:

modelBuilder.Entity
() .HasRequired(x => x.Student) .WithOptional(x => x.Teacher) .Map(x => x.MapKey("TeacherId"));modelBuilder.Entity
();

测试代码:

var teachers = await _teacherRepository.GetAll().Include(x => x.Student).ToListAsync();

生成 SQL 代码:

SELECT     [Extent1].[Id] AS [Id],     [Extent1].[Name] AS [Name],     [Extent1].[StudentCount] AS [StudentCount],     [Extent2].[Id] AS [Id1],     [Extent2].[Name] AS [Name1],     [Extent2].[TeacherId] AS [TeacherId]    FROM  [dbo].[Teachers] AS [Extent1]    INNER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[TeacherId] = [Extent2].[Id]

上面这段代码会执行报错的,因为[Extent1].[TeacherId] = [Extent2].[Id]的 Id 顺序错了,MapKey 配置的是 Teacher,而不是 Student,所以,我们再修改下 Fluent API 配置:

modelBuilder.Entity
();modelBuilder.Entity
() .HasRequired(x => x.Teacher) .WithOptional(x => x.Student) .Map(x => x.MapKey("TeacherId"));

需要注意的是,因为 Teacher 中有了 Student 导航属性,所以我们没有办法再进行 HasForeignKey 的配置。

再次执行测试代码,并没有生成 SQL 代码,而是直接报错:Each property name in a type must be unique. Property name 'TeacherId' is already defined.

根据错误提示,我们去除 Student 中的 TeacherId 属性,重新执行测试代码。

生成的 SQL 代码:

SELECT     [Extent1].[Id] AS [Id],     [Extent1].[Name] AS [Name],     [Extent1].[StudentCount] AS [StudentCount],     [Extent3].[Id] AS [Id1],     [Extent3].[Name] AS [Name1],     [Extent3].[TeacherId] AS [TeacherId]    FROM   [dbo].[Teachers] AS [Extent1]    LEFT OUTER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Id] = [Extent2].[TeacherId]    LEFT OUTER JOIN [dbo].[Students] AS [Extent3] ON [Extent1].[Id] = [Extent3].[TeacherId]

结果是没有什么问题的,但 LEFT OUTER JOIN 了两次,不知道具体是什么原因。

网上找了相关的资料,但一对一关系示例都是那种:子表没有导航属性,主表存储子表的主键作为外键,并有子表的导航属性,上面的类似示例, 找到一个,但评论中的解决方式试过不行。

针对这种情况,如果有更好的实现方式,欢迎告知。

转载地址:http://wwwfa.baihongyu.com/

你可能感兴趣的文章
CocosCreator手记03——简单配置VSCode的TypeScript环境
查看>>
Linux系统服务介绍
查看>>
Exchange Server 2016管理系列课件13.创建和管理邮件用户
查看>>
一个老鸟眼中“IT民工”的发展方向
查看>>
利用五大维度打造真正的「原生广告
查看>>
DB2 V9性能调整之数据库连接数
查看>>
泛娱乐时代:新娱乐方式渐成主流
查看>>
789高手出招,国产手机的崛起路
查看>>
SCMagazine: SIEM走出阴影,迈向光明
查看>>
国内IDC成立SOC安全运营中心
查看>>
Spring 框架是什么?
查看>>
浅谈软件开发定律系列之1:3:9定律
查看>>
linux下文件恢复神器extundelete
查看>>
Javascript语法易出错方面总结
查看>>
部署企业中Windows Server 2008 R2额外域控制器
查看>>
如何处理Dpm备份时“副本内容不一致”的问题
查看>>
模块化安装与删除openstack的dev(control、compute)与folsom(control)版本
查看>>
SCVMM2012部署之三:安装VMM自助服务门户
查看>>
远程管理Windows Server Core的磁盘
查看>>
虚机不能启动的特例思考
查看>>