EF Code-First 学习之旅 配置一对一的关系

1对1、1对0 的关系

例如:Entity1与零个或一个Entity2的实例有关系

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; }
}

在关系型数据库(如SQL Server)中,1对0或1的关系是一个表的主键将是另一个关系表的主键或外键

因此,创建Student表的时候设置StudentId为主键,StudentAddress表的StudentAddressId既是主键有事外键

在Code First默认约定中,StudentId属性默认为Student的主键,StudentAddressId默认为StudentAddress的主键,因此,我们只需要配置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
{
    [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; }
}
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; }
}

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

}

上面的配置说明:StudentAddress在Student中的导航属性是可选的(没有StudentAddress也可以保存Student),Student在StudentAddress中的导航属性是必须的(没有Student的话StudentAddress保存不了),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
{
    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; }
}
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.StudentId); 

}

一对一的关系

一对一在MS SQL Server中在技术上是不可能的,它总是1对0或1的关系,EF是在实体上表现为一对一的关系,而不是在数据库中

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); 

}

modelBuilder.Entity<Student>().HasRequired(s => s.Address)表明Address属性是必须的

.WithRequiredPrincipal(ad => ad.Student)

注:主实体是Student,依赖实体是StudentAddress

时间: 2024-08-07 12:33:50

EF Code-First 学习之旅 配置一对一的关系的相关文章

EF Code First 学习笔记:约定配置

要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就是Fluent API,通过新增相应的配置类来覆盖默认配置.现在我们用这两个来对比了解EF中的约定配置. 主键:KEY Data Annotations:通过Key关键字来标识一个主键 [Key] public int DestinationId { get; set; } Fluent API:

[转载]EF Code First 学习笔记:约定配置

要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就是Fluent API,通过新增相应的配置类来覆盖默认配置.现在我们用这两个来对比了解EF中的约定配置. 主键:KEY Data Annotations:通过Key关键字来标识一个主键 [Key] public int DestinationId { get; set; } Fluent API:

EF Code First学习笔记

EF Code First学习笔记 初识Code First EF Code First 学习笔记:约定配置 Entity Framework 复杂类型 Entity Framework 数据生成选项DatabaseGenerated Entity Framework 并发处理 EF Code First 学习笔记:关系 Entity Framework Code First级联删除 EF Code First 学习笔记:表映射 EF Code First学习笔记:数据库创建 Entity Fr

如何使用EF优雅的配置一对一的关系

在这两天的时间已经有两位同事问到EF(Code First)如何配置一对一的关系,这个说难也不难,说简单吧,一旦设计跑偏那么在Coding的过程中将会很痛苦. 先举个很简单的例子,两个类User和Profile,User里面存在用户的基本信息比如邮箱和密码,Profile里面存放用户的个人资料. public class User { public int Id { get; set; } public string Email { get; set; } public string Passw

EF Code First 学习笔记:表映射

多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Person { [Key] public int PersonId { get; set; } public int SocialSecurityNumber { get; set; } public string FirstName { get; set; } public string LastName

EF Code First学习

1. Code First : 代码优先, 然后根据代码创建数据库: code first的核心是 约定: 2.EF要求 一个类 必须有一个键属性. 规则约定 如果一个属性名为id 或 类名+id, 这一属性 就自动配置为键. 约定 不是必须, 如果不遵从约定 就必须使用 一些配置. 3.code first 使用配置 有两种方法(1).Data Annotations (数据注解)  (2).Fluent API  ; 允许配置各种属性. 关系.继承.级别和数据库映射. (1).Data An

EF Code First 学习笔记:关系

一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就能通过一些引用属性.导航属性等检测到模型之间的关系,自动为我们生成外键.观察下面的类: public class Destination { public int DestinationId { get; set; } public string Name { get; set; } public string Country { get; s

EF Code First学习笔记 初识Code First

Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库. 下面通过一个简单的示例来了解. 建立一个控制台项目.通过Nuget来获取Entity Framework. 增加两个模型类: public class Destination { public int DestinationId { get; set; } public string Name { get; set; }

EF Code First学习笔记:数据库创建

控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayContext. 有几种方法可以改变这种默认约定. 利用配置文件 在配置文件中新加一个连接字符串 <connectionStrings> <add name="BreakAwayContext" providerName="System.Data.SqlClient