Рассмотрим следующие классы:
public class Pie
{
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public ICollection<Pie> Pies { get; set; }
}
У меня есть следующие данные из жестко закодированных файлов:
public class MockPieRepository : IPieRepository
{
private readonly ICategoryRepository _categoryRepository = new MockCategoryRepository();
private List<Pie> pies = new List<Pie>() {
new Pie { Id = 1, Name = "Apple Pie", Price = 12.95M, IsPieOfTheWeek = true, ShortDescription = "Yummy Apple Pie", LongDescription = "Lengthy apple pie", ImageThumbnailUrl = "images/thumbs/apple.jpg", ImageUrl = "images/apple.jpg", CategoryId = 1},
new Pie { Id = 2, Name = "Rhubarb Pie", Price = 11.95M, IsPieOfTheWeek = false, ShortDescription = "Yummy Rhubarb Pie", LongDescription = "Lengthy Rhubarb pie", ImageThumbnailUrl = "images/thumbs/Rhubarb.jpg", ImageUrl = "images/Rhubarb.jpg", CategoryId = 1},
new Pie { Id = 3, Name = "Cheesecake", Price = 8.95M, IsPieOfTheWeek = false, ShortDescription = "Yummy Cheesecake Pie", LongDescription = "Lengthy Cheesecake pie", ImageThumbnailUrl = "images/thumbs/Cheesecake.jpg", ImageUrl = "images/Cheesecake.jpg", CategoryId = 2},
new Pie { Id = 4, Name = "Chocolate Cake", Price = 4.95M, IsPieOfTheWeek = false, ShortDescription = "Yummy Chocolate Pie", LongDescription = "Lengthy Chocolate pie", ImageThumbnailUrl = "images/thumbs/Chocolate.jpg", ImageUrl = "images/Chocolate.jpg", CategoryId = 3},
new Pie { Id = 5, Name = "Mince Beef", Price = 6.99M, IsPieOfTheWeek = false, ShortDescription = "Yummy Mince Beef Pie", LongDescription = "Lengthy Mince Beef pie", ImageThumbnailUrl = "images/thumbs/Beef.jpg", ImageUrl = "images/Beef.jpg", CategoryId = 3}
};
public IEnumerable<Pie> GetPies()
{
return pies.OrderBy(p => p.Id);
}
public Pie GetPieById(int pieId)
{
return pies.FirstOrDefault(p => p.Id == pieId);
}
}
public class MockCategoryRepository : ICategoryRepository
{
public IEnumerable<Category> Categories {
get {
return new List<Category>
{
new Category { CategoryId = 1, CategoryName = "Fruit Pies", Description = "All fruity pies"},
new Category { CategoryId = 2, CategoryName = "Cheesecakes", Description = "Cheesecakes to make your heart sing"},
new Category { CategoryId = 3, CategoryName = "Seasonal Pies", Description = "Christmas, Halloween or Spring"}
};
}
}
}
Когда я пытаюсь получить CategoryName от @ pie.Category.CategoryName, значение равно нулю. Очевидно, что я делаю что-то не так, пытаясь перейти к категории из пирога, но может кто-нибудь сказать мне, что это такое?
Спасибо.