Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

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.

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; }

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.
Updated 1-Apr-17 20:47pm
Bryian Tan 1-Apr-17 22:30pm    
Is the relationship between those two tables defined? ex: Foreign key.
Stein The Ruler 1-Apr-17 22:35pm    
No, nothing else than the standard naming convention. ItemTypeId connects to Id of object ItemType
Bryian Tan 1-Apr-17 22:51pm    
Ok, can you create a Foreign Key Relationships between ItemType and Item table from the database?
Stein The Ruler 1-Apr-17 22:57pm    
thats kinda what Im trying to avoid. I just want to use the built-in functionality that populates a list based on the Id when selecting from the database
Bryian Tan 1-Apr-17 23:13pm    
Ok, then you need to update the code to join those two tables.

join clause (C# Reference)[^]

1 solution

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!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900