原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

EF 6 Code-First系列文章目录:

这里,你将学习在两个实体间,配置一对零或者一对多的关系。
我们使用Student和StudentAddress实体来配置这种关系:

public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; } public virtual StudentAddress Address { get; set; }
} public class StudentAddress
{
public int StudentAddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; } public virtual Student Student { get; set; }
}

你可以看这篇文章,Entity Relationship 来理解EF怎么管理一对一、一对多、多对多关系的。

一对零或一对一的关系发生在这样的两个实体之间:当一个表的主键,是另外一个表的主键并且是外键的时候,所以我们需要配置Student实体中的StudentID属性为主键,然后StudentAddressID既是主键又是外键。

使用数据注解配置一对零或一对一关系

这里,我们将使用数据注解特性来给Student实体和StudentAddress实体,配置一对零或者一对一关系。

Student实体遵循默认的约定,它包含一个StudentId属性,所以到时候这个属性就会成为Students表的主键,Student实体我们不用作任何修改,就让它根据默认配置就行。

对于StudentAddress实体,我们需要配置StudentAddressId为主键和外键,因为StudentAddressId遵循约定,所以只用给它配置外键。

public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; } public virtual StudentAddress Address { get; set; }
} public class StudentAddress
{
[ForeignKey("Student")]
public int StudentAddressId { get; set; } public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; } public virtual Student Student { get; set; }
}

这样就使用数据注解配置了一对零或一对一的关系了。

请注意:Student包含StudentAddress导航属性,并且StudentAddress实体包含Student导航属性。在一对零或一对一关系中,Student可以在没有StudentAddress的情况下被保存,但是StudentAddress没有Student的情况下不能保存。EF将会抛出异常,如果你没有Student实体就保存StudentAddress实体的话。

使用Fluent API配置一对零或者一对一关系

这里我们将使用Fluent API配置一对零或者一对一关系。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Student & StudentAddress entity
modelBuilder.Entity<Student>()
.HasOptional(s => s.Address) // Mark Address property optional in Student entity
.WithRequired(ad => ad.Student); // mark Student property as required in StudentAddress entity. Cannot save StudentAddress without Student
}

上面的例子中,我们从Student实体开始配置,HasOptional()方法配置Student实体中的Address导航属性为可选的【保存Student实体的时候,可以没有StudentAddress实体】,然后WithRequired方法设置StudentAddress实体中的Student导航属性为必须的【保存StudentAddress实体的时候,必须要有Student】。上面的代码,同样会使StudentAddressId成为外键。

这样你就配置了一对零或者一对一的关系,这里Student可以在没有StudentAddress的情况下保存,但是StudentAddress不能在没有Student的情况下保存。EF API将会创建下面的数据库:

使用Fluent API配置一对一的关系

我们可以使用Fluent API配置一对一的关系,这样情况下,两个实体对于彼此都是必须的,意味着:Student必须包含StudentAddress,并且StudentAddress必须包含Student。

请注意:SQL Server中,一对一的关系,在技术上是不可能的。上面的代码设置将会永远是一对零或一对一关系。EF只是对实体形成一对一的关系,并不是在数据库中是一对一关系。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure StudentId as FK for StudentAddress
modelBuilder.Entity<Student>()
.HasRequired(s => s.Address)
.WithRequiredPrincipal(ad => ad.Student); }

上面的代码中,modelBuilder.Entity<Student>().HasRequired(s => s.Address),是设置Student实体中的 StudentAddress导航类型的Address属性是必须的,.WithRequiredPrincipal(ad => ad.Student)是设置StudentAddress实体中的Student属性是必须的。

使用 EF Power Tools为上面的一对一的例子创建实体对象模型,如下:

这一节,一对一的关系就讲解完了,下一节学习一对多的关系。

11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】的更多相关文章

  1. 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...

  2. 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-f ...

  3. 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx EF 6 ...

  4. log4net保存到数据库系列一:WebConfig中配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志 一.WebConfig中配置log4net 二.独立配置文件中配置log4net ...

  5. ABP架构学习系列二:ABP中配置的注册和初始化

    一.手工搭建平台 1.创建项目 创建MVC5项目,手动引入Abp.Abp.Web.Abp.Web.Mvc.Abp.Web.Api 使用nuget添加Newtonsoft.Json.Castle.Cor ...

  6. (DT系列六)devicetree中数据和 struct device有什么关系

    devicetree中数据和structdevice有什么关系 总体来说,devicetree与structdevice的关系应该还是在其生成platformdevice的时候,一直传递的struct ...

  7. 【转】(DT系列六)devicetree中数据和 struct device有什么关系

    原文网址:http://www.cnblogs.com/biglucky/p/4057499.html devicetree中数据和structdevice有什么关系 总体来说,devicetree与 ...

  8. 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx EF 6 Code-Firs ...

  9. 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx EF 6 ...

随机推荐

  1. IDEA VM设置

    1.IDEA vm options -server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128 ...

  2. 10. 批量插入List<String>

    List<String> iscBusOrgIdList = getIscOrgIdList();List<Map<String, Object>> iscBusO ...

  3. 如何创建.gitignore文件,忽略不必要提交的文件

    1.gitignore 在工程实现过程中,会生成一些中间文件,或者在项目中的部分文件是不需要进行版本管理的.对于这些文件应该对于Github来讲是透明的.Github提供这种功能,可以自己指定哪些文件 ...

  4. 最适合入门的Laravel中级教程(四)前端开发

    Laravel 使用 npm 安装前端依赖: npm 是一个类似 composer 的工具: 用于管理前端的各种依赖包: 在使用之前需要先安装 node : Windows 下可以在官网下载安装: h ...

  5. jieba分词/jieba-analysis(java版)

    简介 支持分词模式Search模式,用于对用户查询词分词Index模式,用于对索引文档分词特性支持多种分词模式全角统一转成半角用户词典功能conf 目录有整理的搜狗细胞词库因为性能原因,最新的快照版本 ...

  6. 《Network Security A Decision and Game Theoretic Approach》阅读笔记

    网络安全问题的背景 网络安全研究的内容包括很多方面,作者形象比喻为盲人摸象,不同领域的网络安全专家对网络安全的认识是不同的. For researchers in the field of crypt ...

  7. 转)Ubuntu16.04下安装DDD(Data Display Debugger)

    以下转自:http://www.linuxdiyf.com/linux/26393.html   前两天在Linux论坛偶然间看到了DDD这个软件,根据介绍是一个gdb界面化的调试软件,这正是我找了好 ...

  8. http 连接 analysis service (ssas)

    当数据仓库搭建好后,我们就可以通过sqlserver的管理工具查看服务器上的数据集了.但是这样挺不方便的,如果要远程访问,那么就可以通过http来连接数据仓库.要配置数据仓库http连接非常的简单.如 ...

  9. python note 04 list的应用及元组

    1,昨日内容 ascii:字母,数字,特殊字符:1个字节,8位 Unicode:16位 两个字节 升级 32 位 四个字节 utf-8:最少一个字节 8位表示. 英文字母 8位 1个字节 欧洲16位, ...

  10. (sealed)密封类及密封方法优缺点

    1. 密封类防止被继承 (有利于代码优化, 由于密封类的不被继承性, 代码在搜索此方法时可以直接定位, 不需要一层层的找继承关系) 只有本程序集可以使用 2. 密封类中不需要再写密封方法(一般密封方法 ...