简体   繁体   English

实现实体框架一对一关系

[英]Realize Entity Framework one-to-one relation

I am trying to realize a one-to-one relation in Entity Framework with Code First. 我正在尝试使用Code First在Entity Framework中实现一对一的关系。 I want to create an Airplane table which has a FlightPlan. 我想创建一个包含FlightPlan的飞机表。

Airplane: 飞机:

public class Airplane
{
    [Key]
    public int AirplaneId { get; set; }
    public int Capacity { get; set; }
    public AirplaneStatus AirplaneStatus { get; set; }
    public int AmountOfPassengers { get; set; }
    public int AirfieldId { get; set; }

    public virtual FlightPlan FlightPlan { get; set; }
}

FlightPlan: 飞行计划:

public class FlightPlan
{
    [Key]
    [ForeignKey("Airplane")]
    public int AirplaneId { get; set; }
    public int AirfieldFrom { get; set; }
    public int AirfieldTo { get; set; }
    public int AmountOfPassengers { get; set; }

    public virtual Airplane Airplane { get; set; }
}

When i initialize these classes with Migrations the following table structure is created: 当我使用Migrations初始化这些类时,将创建以下表结构:

在此处输入图片说明

After i manage to create an Airplane with a FlightPlan, which takes some workarounds, the Airplane doesn't have a Flightplan no matter what i try. 在设法通过FlightPlan创建飞机之后,这需要一些解决方法,无论我尝试什么,飞机都没有Flightplan。

在此处输入图片说明

在此处输入图片说明

How can i solve this? 我该如何解决?

Edit 编辑

For testing purposes i add a FlightPlan to every updated Airplane. 为了进行测试,我向每个更新的飞机添加了一个FlightPlan。 After fixing this a route is created for adding a Flightplan. 解决此问题后,将创建一条路线以添加飞行计划。 The code is: 代码是:

var airplane = _context.Airplanes.Find(model.AirplaneId);
var flightplan = new FlightPlan() { AirfieldFrom = 1, AirfieldTo = 2, AmountOfPassengers = 30 };
airplane.FlightPlan = flightplan;
_context.SaveChanges();

The solution for my problem is to force EF to include the FlightPlan when retrieving an Airplane. 解决我的问题的方法是在检索飞机时强制EF包括FlightPlan。

This is most likely due to lazy loading since your FlightPlan is virtual . 这很可能是由于您的FlightPlanvirtual而导致的延迟加载。

var airplane = db.Airplanes.First(); //pull airplane from db
var passengerAmount = airplane.FlightPlan.AmountOfPassengers //attempting to use 
// FlightPlan property after pulling Airplane from db should load the FlightPlan

You can also force EF to include the FlightPlan property when pulling from the db. 从数据库中拉出时,还可以强制EF包括FlightPlan属性。

using System.Data.Entity;
var airplane = db.Airplanes.Include(m => m.FlightPlan).First();

Should be done in the following way: 应该通过以下方式完成:

public class Airplane
{
    [Key]
    public int Id { get; private set; }

    public virtual FlightPlan FlightPlan { get; private set; }

    // ... other properties
}

public class FlightPlan
{
    [Key]
    public int Id { get; private set; }

    public virtual Airplane Airplane { get; private set; }

    // ... other properties
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<FlightPlan>()
       .HasRequired(flightPlan => flightPlan.Airplane)
       .WithOptional(airplane => airplane.FlightPlan);

       base.OnModelCreating(modelBuilder);
}

Your setters of course could be public . 您的二传手当然可以是public

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

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