为什么在此示例中我无法访问CategoryName?

时间:2018-09-10 13:14:05

标签: c# asp.net-core-2.1 navigation-properties

请考虑以下课程:

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时我做错了,但是有人可以告诉我它是什么吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

您从未设置pie.Category = category

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

如果您保存pie并设置了Category,则该数据将保留在数据库中,并且下次加载pie时,您还将拥有pie.Category已加载。由于您还没有从数据库中存储(和检索)数据,所以这没有发生。