EF:1:1关系独立关联 [英] EF: 1:1 relationship independent association

查看:150
本文介绍了EF:1:1关系独立关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用EF代码创建一个简单的1:1关系。



示例:一个人总是拥有一辆车,汽车总是属于一个人。



  public class Person 
{
public Guid Id {get;组; }
public string Name {get;组;
public Car Car {get;组; }
}

// PersonEntityTypeConfiguration
HasKey(k => k.Id);
属性(p => p.Name).IsOptional();
HasRequired(n => n.Car).WithRequiredPrincipal()。WillCascadeOnDelete(true);

汽车:

 code> public class Car 
{
public Guid Id {get;组; }
public string SerialNumber {get;组; }
}

// CarEntityTypeConfiguration
HasKey(k => k.Id);
属性(p => p.SerialNumber).IsOptional();

创建了以下迁移脚本:

  // Up 
CreateTable(
dbo.People,
c => new
{
Id = c。 Guid(nullable:false),
Name = c.String(),
})
.PrimaryKey(t => t.Id);

CreateTable(
dbo.Cars,
c => new
{
Id = c.Guid(nullable:false),
SerialNumber = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey(dbo.People,t => t。 Id,cascadeDelete:true)
.Index(t => t.Id);

我预计EF会生成一个外键,如:

  CreateTable(
dbo.Cars,
c => new
{
Id = c.Guid(nullable: false),
SerialNumber = c.String(),
Person_Id = c.Guid()
})
.PrimaryKey(t => t.Id)
.ForeignKey(dbo.People,t => t.Person_Id,cascadeDelete:true)
.Index(t => t.Id);

为什么EF没有创建这样的外键?



如何让EF生成我期望的脚本?

解决方案

如果你想要 Car 有一个外键 Person 这不是一个 1:1 关联但 1:n (因为 n 汽车可以指相同人们)



你在这里看到的是EF的执行 1:1 关联的方式:主要实体( Person )被复制到依赖实体的主键( Car )。后者也是校长的外键。


I'm trying to create a simple 1:1 relationship with EF code first.

Example: A person always owns a single car and the car always belongs to a single person.

Person:

public class Person
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Car Car { get; set; }
}

// PersonEntityTypeConfiguration
HasKey(k => k.Id);
Property(p => p.Name).IsOptional();
HasRequired(n => n.Car).WithRequiredPrincipal().WillCascadeOnDelete(true);

Car:

public class Car
{
    public Guid Id { get; set; }
    public string SerialNumber { get; set; }
}    

// CarEntityTypeConfiguration
HasKey(k => k.Id);
Property(p => p.SerialNumber).IsOptional();

This created the following migration script:

// Up
CreateTable(
    "dbo.People",
    c => new
        {
            Id = c.Guid(nullable: false),
            Name = c.String(),
        })
    .PrimaryKey(t => t.Id);

CreateTable(
    "dbo.Cars",
    c => new
        {
            Id = c.Guid(nullable: false),
            SerialNumber = c.String(),
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.People", t => t.Id, cascadeDelete: true)
    .Index(t => t.Id);

I expected EF to generate a foreign key like:

CreateTable(
    "dbo.Cars",
    c => new
        {
            Id = c.Guid(nullable: false),
            SerialNumber = c.String(),
            Person_Id = c.Guid()
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.People", t => t.Person_Id, cascadeDelete: true)
    .Index(t => t.Id);

Why EF didn't create a foreign key like that?

How do I tell EF to generate the script that I expect?

解决方案

If you want Car to have a foreign key to Person this is not a 1:1 association but 1:n (because n cars can refer to the same person).

What you see here is EF's way to enforce a 1:1 association: the primary key of the principal entity (Person) is copied to the primary key of the dependent entity (Car). The latter is also a foreign key to the principal.

这篇关于EF:1:1关系独立关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆