C# 包括关联表中的数据
我想问一下如何在我的案例中包含数据。我已经创建了一个关联表,在其中定义C# 包括关联表中的数据,c#,entity-framework,asp.net-core,asp.net-core-mvc,entity-framework-core,C#,Entity Framework,Asp.net Core,Asp.net Core Mvc,Entity Framework Core,我想问一下如何在我的案例中包含数据。我已经创建了一个关联表,在其中定义BusinessGlosItems之间的关系。在下一步中,我创建了BusinessGlosItems的模型,并通过子关系和父关系包含了参考表 最后,我需要显示BusinessGlosItems及其所有相关数据的详细信息。听起来很容易,但我不知道如何解决这个问题。有人能帮忙吗 我的域类如下所示: public class BusinessGlosItem { [DatabaseGenerated(DatabaseGene
BusinessGlosItems
之间的关系。在下一步中,我创建了BusinessGlosItems
的模型,并通过子关系和父关系包含了参考表
最后,我需要显示BusinessGlosItems
及其所有相关数据的详细信息。听起来很容易,但我不知道如何解决这个问题。有人能帮忙吗
我的域类如下所示:
public class BusinessGlosItem
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string BGIName { get; set; }
[InverseProperty("ChildBGItem")]
public ICollection<Reference> ChildReferences { get; set; }
[InverseProperty("ParentBGItem")]
public ICollection<Reference> ParentReferences { get; set; }
}
public class Reference
{
public int Id { get; set; }
[ForeignKey("ChildBGItem")]
public int? ChildGlosItemId { get; set; }
[ForeignKey("ParentBGItem")]
public int? ParentGlosItemId { get; set; }
public int ReferenceTypeId { get; set; }
public virtual ReferenceType ReferenceType { get; set; }
public virtual BusinessGlosItem ChildBGItem { get; set; }
public virtual BusinessGlosItem ParentBGItem { get; set; }
}
BusinessGlosItem = await _context.businessGlosItem
.Include(x => x.BusinessArea)
.Include(x => x.BusinessGlosItemStatus)
.Include(x => x.ChildReferences)
.ThenInclude(x=>x.ReferenceType)
.Include(x => x.ChildReferences) // You missed this include
.ThenInclude(x=>x.ChildBGItem)
.Include(x => x.ChildReferences) // You missed this include
.ThenInclude(x=>x.ParentBGItem)
.Include(x => x.ParentReferences)
.ThenInclude(x=>x.ReferenceType)
.Include(x => x.ParentReferences) // You missed this include
.ThenInclude(x=>x.ChildBGItem)
.Include(x => x.ParentReferences) // You missed this include
.ThenInclude(x=>x.ParentBGItem)
.Include(x=>x.businessGlosItemComments)
.SingleOrDefaultAsync(m => m.Id == id);
提前多谢 您忘了在
ChildReferences
和ParentReferences
中同时提及ChildBGItem
和ParentReferences
的。因此,您的查询应如下所示:
public class BusinessGlosItem
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string BGIName { get; set; }
[InverseProperty("ChildBGItem")]
public ICollection<Reference> ChildReferences { get; set; }
[InverseProperty("ParentBGItem")]
public ICollection<Reference> ParentReferences { get; set; }
}
public class Reference
{
public int Id { get; set; }
[ForeignKey("ChildBGItem")]
public int? ChildGlosItemId { get; set; }
[ForeignKey("ParentBGItem")]
public int? ParentGlosItemId { get; set; }
public int ReferenceTypeId { get; set; }
public virtual ReferenceType ReferenceType { get; set; }
public virtual BusinessGlosItem ChildBGItem { get; set; }
public virtual BusinessGlosItem ParentBGItem { get; set; }
}
BusinessGlosItem = await _context.businessGlosItem
.Include(x => x.BusinessArea)
.Include(x => x.BusinessGlosItemStatus)
.Include(x => x.ChildReferences)
.ThenInclude(x=>x.ReferenceType)
.Include(x => x.ChildReferences) // You missed this include
.ThenInclude(x=>x.ChildBGItem)
.Include(x => x.ChildReferences) // You missed this include
.ThenInclude(x=>x.ParentBGItem)
.Include(x => x.ParentReferences)
.ThenInclude(x=>x.ReferenceType)
.Include(x => x.ParentReferences) // You missed this include
.ThenInclude(x=>x.ChildBGItem)
.Include(x => x.ParentReferences) // You missed this include
.ThenInclude(x=>x.ParentBGItem)
.Include(x=>x.businessGlosItemComments)
.SingleOrDefaultAsync(m => m.Id == id);
现在它应该可以工作了。您正试图显示BusinessGlosItem
及其ChildReferences
和ParentReferences
的详细信息,是吗?或者别的什么?是的,我需要知道孩子和父母的名字好的!现在告诉我您现有的代码有什么问题吗?当我执行此代码时:@foreach(Model.BusinessGlosItem.ParentReferences中的var项){@item.ReferenceType.ReferenceTypeName@item.ParentBGItem.BGIName@item.ChildBGItem.BGIName//这里我想查看ChildItem名称}我收到此错误:NullReferenceException:对象引用未设置为对象的实例。在@item.ChildBGItem.BGIName行