为什么我不能在这个例子中访问CategoryName?

问题描述 投票:-2回答:1

考虑以下类:

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

当我尝试从@ pie.Category.CategoryName获取CategoryName时,该值为null。显然我在尝试从Pie导航到Category时做错了什么,但是有人能告诉我它是什么吗?

谢谢。

c# asp.net-core-2.1 navigation-properties
1个回答
1
投票

你永远不会设置pie.Category = category

EF在加载数据时会自动执行的操作之一是它将“解析”在数据中检测到的导航属性。

如果你用pie集保存Category,那么这些数据将保存在数据库中,下次加载pie时,你也会加载pie.Category。由于您尚未存储(和检索)数据库中的数据,因此未发生这种情况。

© www.soinside.com 2019 - 2024. All rights reserved.