实体框架核心中的列表不会填充 [英] Lists in entity framework core doesnt populate

查看:114
本文介绍了实体框架核心中的列表不会填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我有两节课; Item和ItemType。

Item包含ItemTypeId,ItemType包含Items上的列表。



但是Items列表永远不会被填充。总是0.



项目

公共类项目
{
public int Id {get;组; }
public string Name {get;组; }
public string Graphic {get;组; }
公共小数价格{get;组; }
public string说明{get;组; }
public int ItemTypeId {get;组; }
public virtual ItemType ItemType {get;组; }
}





ItemType

 public class ItemType 
{
public int Id {get;组; }
public string Name {get;组; }

public virtual ICollection< Item>物品{get;组; } = new HashSet< Item>();

public enum ListTypes
{
Student = 1,
Work = 2,
Gamer = 3,
Other = 4
}
}





这是选择ItemType的视图组件:

 public class ItemListViewComponent:ViewComponent 
{
private Data.TechDbContext _context;

public ItemListViewComponent(Data.TechDbContext context)
{
_context = context;
}

public async任务< IViewComponentResult> InvokeAsync(Models.ItemType.ListTypes类型)
{
var single = await(来自_context.ItemTypes中的t,其中t.Id ==(int)类型选择t).FirstAsync< Models.ItemType>( );

return View(single);
}
}





找到正确的ItemType,但Items属性始终为0.(是的我已经破坏了数据库中的数据。



任何帮助都将受到高度赞赏! (因为它让我发疯了)



我尝试过:



我尝试过调试和选择数据的不同方法。删除数据库并重新填充数据。

解决方案

我一定错过了关于加载实体的部分。



我向我的DbContext添加了一个GetItemType方法,它加载了关系数据

 public async Task< ItemType> GetItemType(ItemType.ListTypes type)
{
ItemType result = await this.ItemTypes.Include(i => i.Items).SingleAsync(i => i.Id ==(int)type );

返回结果;
}





现在我可以发送一个ListType并获取一个填充的对象。太好了!

Hi!

I have two classes; Item and ItemType.
Item contains ItemTypeId and ItemType contains a list over Items.

But the Items list never gets populated. Its always 0.

Item:

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Graphic { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
    public int ItemTypeId { get; set; }
    public virtual ItemType ItemType { get; set; }
}



ItemType:

public class ItemType
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Item> Items { get; set; } = new HashSet<Item>();

    public enum ListTypes
    {
        Student = 1,
        Work = 2,
        Gamer = 3,
        Other = 4
    }
}



and this is the View Component that selects ItemType:

public class ItemListViewComponent : ViewComponent
    {
        private Data.TechDbContext _context;

        public ItemListViewComponent(Data.TechDbContext context)
        {
            _context = context;
        }

        public async Task<IViewComponentResult> InvokeAsync(Models.ItemType.ListTypes type)
        {
            var single = await (from t in _context.ItemTypes where t.Id == (int)type select t).FirstAsync<Models.ItemType>();

            return View(single);
        }
    }



It finds the correct ItemType, but the Items property are always 0. (yes I've checeked theres data in the db)

Any help with this will be highly appreciated! (since its driving me crazy)

What I have tried:

I've tried debugging and different methods of selecting data. Drop the database and repopulated the data.

解决方案

I must have missed the part about loading entities.

I added a GetItemType method to my DbContext which loads the relational data

public async Task<ItemType> GetItemType(ItemType.ListTypes type)
{
    ItemType result = await this.ItemTypes.Include(i => i.Items).SingleAsync(i => i.Id == (int)type);

    return result;
}



So now I can send in a ListType and get a populated object back. Great!


这篇关于实体框架核心中的列表不会填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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