简体   繁体   English

在 EF6 中使用 PK 而不是 FK 创建可选-可选关系

[英]Create optional-optional relationship in EF6 with PK instead of FK

Is it possible to create a mapping which would fill my navigation property automatically, based on table's PK?是否可以根据表的 PK 创建一个自动填充我的导航属性的映射? That means I don't want to have a FK on any table.这意味着我不想在任何桌子上都有 FK。 I know it is possible when there is Optional -> Required mapping.我知道当有 Optional -> Required 映射时这是可能的。 AFAIK no constraint is created on SQL Server, the relationship is "simulated" by EF6 in runtime. AFAIK 在 SQL 服务器上没有创建任何约束,EF6 在运行时“模拟”了这种关系。

|====================|                   |====================|
|        Car         |                   |       Driver       |
|====================|                   |====================|
|         Id         |  0..1 ----- 0..1  |         Id         |
|--------------------|                   |--------------------|

So that code _dbContext.Cars.Include(x => x.Driver).First(x => x.Id == 1);所以代码_dbContext.Cars.Include(x => x.Driver).First(x => x.Id == 1); would return a car with driver if exists, or null driver if doesn't exist.如果存在,将返回带有驱动程序的汽车,如果不存在,则返回 null 驱动程序。

class Car
{
    public int Id { get; set; }
    public virtual Driver Driver { get; set; }
}

class Driver
{
    public int Id { get; set; }
    public virtual Car Car { get; set; }
}

The following mappings wants to create a DriverId in my Car:以下映射想要在我的汽车中创建一个 DriverId:

class CarMapping : EntityTypeConfiguration<Car>
{
    public CarMapping()
    {
        HasKey(x => x.Id);
    }
}

class DriverMapping : EntityTypeConfiguration<Driver>
{
    public CarMapping()
    {
        HasKey(x => x.Id);
        HasOptional(x => x.Car)
            .WithOptionalPrincipal(x => x.Driver);
    }
}

EDIT编辑

I checked with mentioned earlier required mapping:我检查了前面提到的required映射:

class CarMapping : EntityTypeConfiguration<Car>
{
    public CarMapping()
    {
        HasKey(x => x.Id);
    }
}

class DriverMapping : EntityTypeConfiguration<Driver>
{
    public CarMapping()
    {
        HasKey(x => x.Id);
        HasOptional(x => x.Car)
            .WithRequired(x => x.Driver);
    }
}

And it works.它有效。 EF6 does not perform any validation on Inserts nor Deletes... Is this a bug? EF6 不对插入或删除执行任何验证...这是一个错误吗? Well at least it works for my case.好吧,至少它适用于我的情况。

As far as I understand you want a one-to-one relationship between a Car and a Driver ie each Car can have zero or one driver.据我了解,您希望 Car 和 Driver 之间存在一对一的关系,即每辆 Car 可以有零个或一个驱动程序。 If so you need to define the Driver's primary key as a foreign key to the Car's entity.如果是这样,您需要将 Driver 的主键定义为 Car 实体的外键。 Have you tried this你试过这个

class Driver
{
   [ForeignKey("Car")]
   public int Id { get; set; }
   public virtual Car Car { get; set; }
}

This way only a Car record will have only one Driver record referencing it at the same time.这样,只有一个 Car 记录将只有一个 Driver 记录同时引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM