Configure One-to-Zero-or-One Relationship:

Here, we will configure One-to-Zero-or-One relationship between two entities, e.g. Entity1 can be associated with zero or only one instance of Entity2.

Take an example of the following Student and StudentAddress entities.

public class Student
{
public 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; }
}

Visit Entity Relationship section to understand how EF manages one-to-one, one-to-many, and many-to-many relationships.

Now, let's configure Student and StudentAddress entities for One-to-Zero-or-One relationship where Student can have zero or maximum one StudentAddress.

As you may know, a one-to-zero-or-one relationship happens when the primary key of one table becomes PK & FK in another table in relational database such as SQL Server. So, we need to configure above entities in such a way that EF creates Students and StudentAddresses table in the DB where it will make StudentId in Student table as PK and StudentAddressId column in StudentAddress table as PK and FK both.

Configure one-to-zero-or-one relationship using DataAnnotations:

Here, we will apply DataAnnotations attributes on Student and StudentAddress entities to establish one-to-zero-or-one relationship.

The Student entity follows the default code-first convention as it includes StudentId property which will be key property. So we don't need to apply any attributes on it because EF will create Students table and make StudentId as a primary key in the DB.

For the StudentAddress entity, we need to configure StudentAddressId as PK & FK both. StudentAddressId property follows the default convention for primary key. So we don't need to apply any attribute for PK. However, we also need to make it a foreign key which points to StudentId. So, apply [ForeignKey("Student")] on StudentAddressId property which will make it foreign key for Student entity as shown below.

public class Student
{
public 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; }
}

Thus, Student and StudentAddress entities has One-to-Zero-or-One relationship.

When StudentAddress entity does not follow conventions:

If, for example, StudentAddress entity does not follow the convention for PK i.e. different name for Id property then you need to configure it for PK as well. Consider the following StudentAddress entity which has property name StudentId instead of StudentAddressId.

public class Student
{
public Student() { } public int StudentId { get; set; }
public string StudentName { get; set; } public virtual StudentAddress Address { get; set; } } public class StudentAddress
{
[Key, ForeignKey("Student")]
public int StudentId { 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; }
}

In the above example, we need to configure StudentId property as Key as well as ForeignKey. This will make StudentId property in StudentAddress entity as PK and FK both.

Note: Student include StudentAddress navigation property and StudentAddress includes Student navigation property. With one-to-zero-or-one relationship, Student can be saved without StudentAddress but StudentAddress entity cannot be saved without Student entity. EF will throw an exception if you try to save StudentAddress entity without Student entity.

Configure One-to-Zero-or-One relationship using Fluent API:

Here, we will use Fluent API to configure Student and StudentAddress entities. Please note that we will not apply any DataAnnotations attributes in Student and StudentAddress entities because we will use Fluent API to configure them.

When Student and StudentAddress follow the conventions:

Student and StudentAddress entities follow the default code-first convention for PrimaryKey. So, we don't need to configure them to define their PrimaryKeys. We only need to configure StudentAddress entity where StudentAddressId should be ForeignKey.

The following example sets one-to-zero or one relationship between Student and StudentAddress using 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 }

In the above example, Student entity is configured using HasOptional() method which indicates that the StudentAddress navigation property in Student entity is an optional (not required when saving Student entity). Then, WithRequired() method configures StudentAddress entity and make Student navigation property of StudentAddress as required (required when saving StudentAddress entity. It will throw an exception when StudentAddress entity is saving without Student navigation property). This will make StudentAddressId as ForeignKey also.

Thus, you can configure One-to-Zero-or-one relationship between two entities where Student entity can be saved without attaching StudentAddress object to it but StudentAddress entity cannot be saved without attaching an object of Student entity. This makes one end required.

When StudentAddress entity do not follow conventions:

Now, let's take an example of StudentAddress entity where it does not follow primary key convention i.e. have different Id property name than <type name>Id. Consider the following Student and StudentAddress entities.

public class Student
{
public Student() { } public int StudentId { get; set; }
public string StudentName { get; set; } public virtual StudentAddress Address { get; set; } } public class StudentAddress
{
public int StudentId { 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; }
}

So now, we need to configure StudentId property of StudentAddress for PrimaryKey of StudentAddress as well as ForeignKey as shown below.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure StudentId as PK for StudentAddress
modelBuilder.Entity<StudentAddress>()
.HasKey(e => e.StudentId); // Configure StudentId as FK for StudentAddress
modelBuilder.Entity<Student>()
.HasOptional(s => s.Address)
.WithRequired(ad => ad.Student); }

Configure One-to-One relationship using Fluent API:

We can configure One-to-One relationship between entities using Fluent API where both ends are required, meaning Student entity object must include StudentAddress entity object and StudentAddress entity must include Student entity object in order to save it.

Note:One-to-one relationship is technically not possible in MS SQL Server. It will always be one-to-zero or one. EF forms One-to-One relationships on entities not in DB.

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

In the above example, modelBuilder.Entity<Student>().HasRequired(s => s.Address) makes Address property of StudentAddress is required. .WithRequiredPrincipal(ad => ad.Student) makes Student property of StudentAddress entity as required. Thus it configures both ends required. So now, when you try to save Student entity without address or StudentAddress entity without Student, it will throw an exception.

Note: Here, Principal entity is Student and dependent entity is StudentAddress.

DataAnnotations and Fluent API example for one-to-zero or one relationship will create the following database:

You can check the relationship between Student and StudentAddress in the database, as shown below:

If you create an entity data model of a created database then it will appear like the diagram shown below:

Learn how to configure a one-to-many relationship in the next section.

Entity Framework Code-First(11):Configure One-to-One的更多相关文章

  1. Entity Framework Tutorial Basics(11):Code First

    Code First development with Entity Framework: Entity Framework supports three different development ...

  2. Entity Framework Code first(转载)

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

  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 (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  6. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

  7. Entity Framework Code First (四)Fluent API - 配置属性/类型

    上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...

  8. Entity Framework Code First (八)迁移 Migrations

    创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...

  9. Entity Framework Code First (六)存储过程

    声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映 ...

  10. Entity Framework Code First (五)Fluent API - 配置关系

    上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...

随机推荐

  1. jquery中篇

    一.attr 返回属性值 返回被选元素的属性值. 语法 $(selector).attr(attribute) 参数 描述 attribute 规定要获取其值的属性. 属性 • 属性 o attr(n ...

  2. POJ 2230 Watchcow 【欧拉路】

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6336   Accepted: 2743   Specia ...

  3. Spring中ApplicationContext和beanfactory区别

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  4. 算法(Algorithms)第4版 练习 1.3.4

    主要思路: 遇到左括号则一直压栈,遇到右括号时则从栈中弹出一个元素. 如果此时栈为空,则返回false. 如果这个元素与右括号不匹配,则返回false. 重复此过程,最后判断栈是否为空,若为空则返回t ...

  5. spring boot项目启动报(No session repository could be auto-configured, check your configuration (session store type is 'null'))

    找到项目的application配置文件,增加 spring.session.store-type=none,重新启动问题解决 注:因为项目未使用redis管理session,可以如上设置,如果想使用 ...

  6. $().bind()的返回值

    var eleMenus = $("#choMenu a").bind("click", function (event){}); 此时eleMeuns的值是$ ...

  7. 英语发音规则---ea字母组合发音规律

    英语发音规则---ea字母组合发音规律 一.总结 一句话总结:字母组合ea的发音规律,在学习字母组合在单词中的发音规律以前,一定要熟练撑握什么是开音节,什么是闭音节,否则你就不撑握这些发音规律. ea ...

  8. web项目中添加logger日志

    在项目中添加log4j.xml文件 log4j.xml文件 <?xml version="1.0" encoding="UTF-8" ?><! ...

  9. java:练习学校学生

    java:练习学校学生 一个学生对应一个学校 一个学校对应多个学生 Student类,School类,Demo测试类 Student: public class Student { private S ...

  10. Selenium-一组元素的定位

    一组元素的定位: 有时候我们可能需要定位一组元素,比如一组checkbox,这时候要实现的思路大概为: 先把一组元素识别出来,然后定位你需要的元素 下面是查找多个元素(这些方法将返回一个列表): 方法 ...