EF6急切加载有哪些要求?

时间:2016-06-22 19:12:45

标签: c# entity-framework

我似乎设置正确,但是子对象返回null。甚至SQL输出也显示了连接(是的,设置了要加入的键):

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[Description] AS [Description], 
[Extent1].[IsVesselizable] AS [IsVesselizable], 
[Extent1].[Price] AS [Price], 
[Extent1].[CategoryId] AS [CategoryId], 
[Extent2].[Id] AS [Id1], 
[Extent2].[Name] AS [Name1]
FROM  [dbo].[MenuItems] AS [Extent1]
INNER JOIN [dbo].[MenuItemCategories] AS [Extent2] ON [Extent1].[CategoryId] = [Extent2].[Id]

这是API函数:

    // GET: api/MenuItems
    public IQueryable<MenuItem> GetMenuItems()
    {
        return db.MenuItems.Include(m => m.Category);
    }

和一些MenuItem类:

public class MenuItem
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    [Required]
    public decimal Price { get; set; }

    [Required]
    public int CategoryId { get; set; }

    public MenuItemCategory Category { get; set; }
}
EF似乎应该让一切变得如此简单,但显然我在这里错过了一些东西。我不需要像这样制作最后一行,是吗?

public MenuItemCategory MenuItemCategory { get; set; }

0 个答案:

没有答案