EF外键使用流利的API [英] EF Foreign Key using Fluent API

查看:277
本文介绍了EF外键使用流利的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的模型。我有一对一的映射对车辆和驾驶员。我将有车辆首先创建,然后映射驱动车辆。

Here are my models. I have one to one mapping for Vehicle and Driver. I will have the vehicle created first and then map the driver to the vehicle.

public class Driver
{
    public int Id { get; set; }
    public String Name { get; set; }
    public int VehicleId { get; set; }
    public virtual Vehicle Vehicle  { get; set; }
}

public class Vehicle
{  
    public int Id { get; set; }
    public String Name { get; set; }

    public virtual Driver Driver { get; set; }

    public int VehicleGroupId { get; set; }
    public virtual VehicleGroup Vehicles { get; set; }
}



我想在Driver类使用VehicleId属性来保持车辆的ID 。司机是驾驶

I want to use VehicleId property in Driver class to keep id of vehicle the driver is driving.

我已经写了下面流利的API代码:

I've written the following Fluent API code:

modelBuilder.Entity<Vehicle>()
            .HasRequired(d => d.Driver)
            .WithRequiredPrincipal();



但它会在驱动程序表中的新列 - Vehicle_VehicleId并将其映射到的车辆表VehicleId。我希望司机表的VehicleId映射。

But it creates a new column in Drivers table - Vehicle_VehicleId and maps it to the VehicleId on Vehicle table. I want the VehicleId of Driver table to map.

另外,我是全新的,以EF和流利的API。我觉得WithRequiredDependent和WithRequiredPrincipal之间极其混乱选择。会很高兴,如果你能在简单的词形容它。谢谢

Also, i'm brand new to EF and Fluent API. I find it extremely confusing choosing between WithRequiredDependent and WithRequiredPrincipal. Would be glad if you can describe it in simple words. Thanks.

推荐答案

这行:

公众诠释VehicleId {搞定;组; }

public int VehicleId { get; set; }

告诉EF,的通过代码的约定的,要在驱动程序指向外键。

is telling EF, through code-conventions, that you want a foreign key in Driver pointing to Vehicle.

这人说告诉EF你想要一个1:驱动程序汽车

This one is saying telling EF that you want a 1:1 relationship from Driver to Vehicle:

public虚拟车辆车辆{搞定;组; }

public virtual Vehicle Vehicle { get; set; }

您应该删除这两个和你流利的API配置坚持下去。

You should remove both and stick with your Fluent API configuration.

WithRequiredPrincipal 主场迎战的 WithRequiredDependent

Regarding WithRequiredPrincipal vs. WithRequiredDependent:

您指定车和驱动程序,从导航到驱动程序,即:车辆1 - > 1 驱动程序

You are specifying a compulsory relationship between Vehicle and Driver, with navigation from Vehicleto Driver, thus: Vehicle 1 --> 1 Driver

(车辆的本金和驱动程序的依赖,因为导航物业位于和指向驱动程序

(Vehicle is the principal and Driver the dependent, since the navigation property is located in Vehicleand pointing to Driver .)

modelBuilder.Entity<Vehicle>()
            .HasRequired(d => d.Driver)
            .WithRequiredDependent();

您指定车辆之间的强制关系驱动程序,从导航驱动程序汽车,从而: 1< - 1 驱动程序

You are specifying a compulsory relationship between Vehicle and Driver, with navigation from Driver to Vehicle, thus: Vehicle 1 <-- 1 Driver

是依赖和驱动程序校长,因为导航物业位于驱动程序指向

(Vehicle is the dependent and Driver the principal, since the navigation property is located in Driver pointing to Vehicle.)

这两个是相似的:

modelBuilder.Entity<Vehicle>()
            .HasRequired(v => v.Driver)
            .WithRequiredPrincipal();

modelBuilder.Entity<Driver>()
            .HasRequired(d => d.Vehicle)
            .WithRequiredDependent();

这篇关于EF外键使用流利的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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