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

查看:119
本文介绍了EF外键使用Fluent 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.

我写了以下Fluent API代码:

I've written the following Fluent API code:

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

但是它会在Drivers表 - Vehicle_VehicleId中创建一个新列,并将其映射到Vehicle表上的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和Fluent 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.

推荐答案

此行:


public int VehicleId {get;组; }

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:1的关系从司机车辆

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


public virtual Vehicle Vehicle {get;组; }

public virtual Vehicle Vehicle { get; set; }

您应该删除两者并坚持使用Fluent 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

(车辆是主体,驱动程序 code>驱动程序。)

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

您正在指定 Vehicle 之间的强制性关系和驱动程序,导航从驱动程序车辆,因此:车辆 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外键使用Fluent API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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