我是Entity Framework的新手,所以我不知道我做得对,或者这是新Entity Framework Core中的错误。我需要为相关页面做一个类,例如1页与另一页相关。我上课a <- ifelse(data$category == 0, "red", "blue")
ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = "identity", aes(fill = category)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, colour = a))
Page
这是我public class Page
{
public int ID { get; set; }
public string UrlName { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
public string Content { get; set; }
public List<RelatedPages> RelPages1 { get; set; }
public List<RelatedPages> RelPages2 { get; set; }
}
的课程:
RelatedPages
我希望从public class RelatedPages
{
public int ID { get; set; }
public int? Page1ID { get; set; }
public Page Page1 { get; set; }
public int? Page2ID { get; set; }
public Page Page2 { get; set; }
}
获取Page
实例,反之,与RelatedPages
类似,但我有NullReferenceException。
这是context.RelatedPages.Single(r => r.ID==1).Page2.UrlName
OnModelCreating
中的DbContext
:
modelBuilder.Entity<RelatedPages>()
.HasOne(x => x.Page1)
.WithMany(x => x.RelPages1)
.HasForeignKey(x => x.Page1ID);
modelBuilder.Entity<RelatedPages>()
.HasOne(x => x.Page2)
.WithMany(x => x.RelPages2)
.HasForeignKey(x => x.Page2ID);
答案 0 :(得分:1)
尝试加入Page2
。
将context.RelatedPages.Single(r => r.ID==1).Page2.UrlName
更改为context.RelatedPages.Include(x=>x.Page2).Single(r => r.ID==1).Page2.UrlName