C# 为什么可以';在本例中,我不能访问CategoryName吗?

C# 为什么可以';在本例中,我不能访问CategoryName吗?,c#,asp.net-core-2.1,navigation-properties,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 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; }
}
公共类派
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int CategoryId{get;set;}
公共虚拟类别{get;set;}
}
公共类类别
{
public int CategoryId{get;set;}
公共字符串CategoryName{get;set;}
公共字符串说明{get;set;}
公共ICollection 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"}
            };
        }
    }
}
公共类存储库:IPieRepository
{
private readonly ICategoryRepository _categoryRepository=new-MockCategoryRepository();
私有列表pies=新列表(){
新馅饼{Id=1,Name=“Apple Pie”,Price=12.95M,ispieofweek=true,ShortDescription=“Yummy Apple Pie”,LongDescription=“long Apple Pie”,ImageThumbnailUrl=“images/thumbs/Apple.jpg”,ImageUrl=“images/Apple.jpg”,CategoryId=1},
新馅饼{Id=2,Name=“Rhubarb Pie”,Price=11.95M,ispieofweek=false,ShortDescription=“Yummy Rhubarb Pie”,LongDescription=“long Rhubarb Pie”,ImageThumbnailUrl=“images/thumbs/Rhubarb.jpg”,ImageUrl=“images/Rhubarb.jpg”,CategoryId=1},
新馅饼{Id=3,Name=“Cheesecake”,Price=8.95M,ispieofweek=false,ShortDescription=“Yummy Cheesecake Pie”,LongDescription=“long Cheesecake Pie”,ImageThumbnailUrl=“images/thumbs/Cheesecake.jpg”,ImageUrl=“images/Cheesecake.jpg”,CategoryId=2},
新馅饼{Id=4,Name=“巧克力蛋糕”,Price=4.95M,ispieofweek=false,ShortDescription=“美味巧克力馅饼”,longsdescription=“冗长巧克力馅饼”,ImageThumbnailUrl=“images/thumbs/Chocolate.jpg”,ImageUrl=“images/Chocolate.jpg”,CategoryId=3},
新馅饼{Id=5,Name=“Mince-Beef”,Price=6.99M,ispieofweek=false,ShortDescription=“Yummy-Mince-Beef-Pie”,longsdescription=“long-Mince-Beef-Pie”,ImageThumbnailUrl=“images/thumbs/Beef.jpg”,ImageUrl=“images/Beef.jpg”,CategoryId=3}
};
公共IEnumerable GetPies()
{
返回pies.OrderBy(p=>p.Id);
}
公共派GetPieById(int-pieId)
{
返回pies.FirstOrDefault(p=>p.Id==pieId);
}
}
公共类MockCategoryRepository:ICategoryRepository
{
公共可数范畴{
得到{
返回新列表
{
新类别{CategoryId=1,CategoryName=“水果派”,Description=“所有水果派”},
新类别{CategoryId=2,CategoryName=“Cheesecakes”,Description=“Cheesecakes to make your heart sing”},
新类别{CategoryId=3,CategoryName=“季节性馅饼”,Description=“圣诞节、万圣节或春天”}
};
}
}
}
当我尝试从@pie.Category.CategoryName获取CategoryName时,该值为null。显然,我在尝试从Pie导航到Category时出错了,但有人能告诉我它是什么吗


谢谢。

您从未设置过
pie.Category=Category

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


如果将
pie
Category
集一起保存,则该数据将保留在数据库中,下次加载
pie
时,也将加载
pie.Category
。由于您没有从数据库中存储(和检索)数据,因此没有发生这种情况。

我在代码中没有看到
@pie.Category.CategoryName
。我假设您通过引用类别(例如,通过设置
CategoryId=1
)将类别自动应用于
pie
,情况并非如此。当然,您必须自己将带有该id的类别应用于
饼图
。类似于
myPie=newpie{Category=getCategoryById(categoryId)}
的东西。那么什么是
@Pie
?有错误吗?还有别的吗?@HimBromBeere是的,这就是我的意思。以前,我使用了实体框架数据库,并且能够对饼图和类别之间的关联进行分析。显然,这并没有像我预期的那样发生在这里。没问题。谢谢你抽出时间谢谢你抽出时间。我不是在使用数据库,而是在内存中。我现在意识到我试图解释的是行不通的。