EF Core:急切加载和延迟加载都失败了(潜在的错误)

时间:2016-10-18 18:43:39

标签: c# entity-framework entity-framework-core

我正在使用最新版本的EF Core,它无法正常加载导航属性。 假设以下实体类

public partial class Categories
{
    public Categories()
    {
        Inventories = new HashSet<Inventories>();
    }

    public string CategoryId { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Inventories> Inventories { get; set; }
}
public partial class Inventories
{
    public Inventories()
    {
        OrderItems = new HashSet<OrderItems>();
    }

    public string InventoryId { get; set; }
    ..
    public string CategoryId { get; set; }
    public virtual Categories Category { get; set; }
}

以下操作无法将Category实体附加到查询中:

var query = Db.Set<Inventories>();
//query.FirstOrDefault().Category is Null
query.Include(x=> x.Category);
//query.FirstOrDefault().Category is Null
query.Include("Category");
//query.FirstOrDefault().Category is Null

但是,如果我这样做,Lazy和Eager加载工作:

var query = Db.Set<Categories>();
var query2 = Db.Set<Inventories>();
//query2.FirstOrDefault().Category Has Value

0 个答案:

没有答案