Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包括关联表中的数据_C#_Entity Framework_Asp.net Core_Asp.net Core Mvc_Entity Framework Core - Fatal编程技术网

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行