Hi!
Im trying to make a really simple model that consists of two tables; ItemType and Item. The relationship is that Item will always be some kind of ItemType.
But when I select the ItemType from the database the Items property doesnt get populated and contains 0 elements.
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
}
}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; }
}
so this is pretty basic. I want to select a ItemType and have the Items property filled with Items that has equal ItemTypeId.
this is the code where I select and use the database
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);
}
}and if its of any interest, the dbcontext
public class TechDbContext : DbContext
{
public TechDbContext(DbContextOptions<TechDbContext> options) : base(options)
{
}
public DbSet<ItemType> ItemTypes { get; set; }
public DbSet<Item> Items { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ItemType>().ToTable("ItemType");
modelBuilder.Entity<Item>().ToTable("Item");
}
}Maybe I've misunderstood the concept, but this is how its supposed to work, right??
Any help will be HIGHLY appreciated, since I'm going slightly mad.