C# 当项具有同一类的多个导航属性时,Linq Include不工作

C# 当项具有同一类的多个导航属性时,Linq Include不工作,c#,asp.net-mvc,entity-framework,linq,ef-code-first,C#,Asp.net Mvc,Entity Framework,Linq,Ef Code First,我正在为学校做一个项目,在这个项目上有点卡住了 预订有3个导航属性、1个客户和2个机场。 要在其中一个视图中获取预订的CustomerCode,我可以使用(db.Bookings.Include(b=>b.Customer))。 当我尝试对始发地和/或目的地(db.Bookings.Include(b=>b.Origin))执行相同操作时,什么都没有发生 我可以通过使用第二个查询查找和设置起点和终点来解决这个问题。(booking.Origin=db.Airports.Find(id)) 但是我

我正在为学校做一个项目,在这个项目上有点卡住了

预订有3个导航属性、1个客户和2个机场。 要在其中一个视图中获取预订的CustomerCode,我可以使用(
db.Bookings.Include(b=>b.Customer)
)。 当我尝试对始发地和/或目的地(
db.Bookings.Include(b=>b.Origin)
)执行相同操作时,什么都没有发生

我可以通过使用第二个查询查找和设置起点和终点来解决这个问题。(
booking.Origin=db.Airports.Find(id)
) 但是我想知道为什么包含不起作用,以及是否有更优雅的方式在预订中加载机场

预约班

public int BookingID { get; set; }
public int CustomerID { get; set; }
public int OriginID { get; set; }
public int DestinationID { get; set; }

public string Awb { get; set; }
public string ClientRef { get; set; }
public string Info { get; set; }

// Navigation
public virtual Airport Origin { get; set; }
public virtual Airport Destination { get; set; }
public virtual Customer Customer { get; set; }
public int BookingID { get; set; }
public int CustomerID { get; set; }
[ForeignKey("Origin")]
public int OriginID { get; set; }
[ForeignKey("Destination")]
public int DestinationID { get; set; }

public string Awb { get; set; }
public string ClientRef { get; set; }
public string Info { get; set; }

// Navigation
public virtual Airport Origin { get; set; }
public virtual Airport Destination { get; set; }
public virtual Customer Customer { get; set; }
客户类别

public int CustomerID { get; set; }

public string CustomerCode { get; set; }
public string CompanyName { get; set; }
public string VatNumber { get; set; }
机场舱

public int AirportID { get; set; }

public string AirportCode { get; set; }
控制器

public ActionResult Index()
{
    var bookings = db.Bookings.Include(b => b.Origin).Include(b => b.Destination).Include(b => b.Customer);
    return View(bookings.ToList());
}
上下文

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

public class AppContext : DbContext
{
    // You can add custom code to this file. Changes will not be overwritten.
    // 
    // If you want Entity Framework to drop and regenerate your database
    // automatically whenever you change your model schema, please use data migrations.
    // For more information refer to the documentation:
    // http://msdn.microsoft.com/en-us/data/jj591621.aspx

    public AppContext() : base("name=AppContext")
    {
    }

    public System.Data.Entity.DbSet<Tester.Models.Country> Countries { get; set; }

    public System.Data.Entity.DbSet<Tester.Models.Airport> Airports { get; set; }

    public System.Data.Entity.DbSet<Tester.Models.Customer> Customers { get; set; }

    public System.Data.Entity.DbSet<Tester.Models.Booking> Bookings { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;
公共类AppContext:DbContext
{
//您可以将自定义代码添加到此文件。更改不会被覆盖。
// 
//如果希望实体框架删除并重新生成数据库
//无论何时更改模型架构,都会自动执行数据迁移。
//有关更多信息,请参阅文档:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public AppContext():base(“name=AppContext”)
{
}
public System.Data.Entity.DbSet Countries{get;set;}
public System.Data.Entity.DbSet Airports{get;set;}
public System.Data.Entity.DbSet客户{get;set;}
public System.Data.Entity.DbSet Bookings{get;set;}
}

原点是一个属性。不可能用于include。也许你不喜欢尝试这样:

     Booking.Include(a=> a.Airport)

根据Alexander Derck的建议,使用
[ForeignKey]
属性

预约班

public int BookingID { get; set; }
public int CustomerID { get; set; }
public int OriginID { get; set; }
public int DestinationID { get; set; }

public string Awb { get; set; }
public string ClientRef { get; set; }
public string Info { get; set; }

// Navigation
public virtual Airport Origin { get; set; }
public virtual Airport Destination { get; set; }
public virtual Customer Customer { get; set; }
public int BookingID { get; set; }
public int CustomerID { get; set; }
[ForeignKey("Origin")]
public int OriginID { get; set; }
[ForeignKey("Destination")]
public int DestinationID { get; set; }

public string Awb { get; set; }
public string ClientRef { get; set; }
public string Info { get; set; }

// Navigation
public virtual Airport Origin { get; set; }
public virtual Airport Destination { get; set; }
public virtual Customer Customer { get; set; }

我不懂这行:
db.Airports.Include(b=>b.Origin)
。从
机场
模型中,您尝试包含
预订
的属性?如果只是打字错误,你能告诉我你有没有流畅的地图?您没有指定
ForeignKey
属性(由于Id命名约定,通常不需要,但谁知道呢,因为您有2x导航属性),因此这可能是问题所在。此代码没有问题。你能显示上下文类吗?@AlexanderDerck:这是一个打字错误,应该是db.Bookings。我会试试[ForeignKey]@YacoubMassad:see update我测试了你的代码,效果很好。你说“什么都没发生”是什么意思?这是否意味着
Origin
的值为空?这应该是注释,而不是答案。