简体   繁体   English

在实体框架核心的多对多关系中获取导航属性时出错

[英]Error in getting navigation property in a many to many relationship in entity framework core

I have two entities that are connected in a many to many relationship as follows我有两个以多对多关系连接的实体,如下所示

public class Fee
{
    public int Id { get; set; }
    [Required]
    public string Label { get; set; }
    public string Description { get; set; }

   ----Ignored for brevity----

    public virtual ICollection<ClassFee> ClassFees { get; set; }

}

public class StudentClass
{

    public int Id { get; set; }
    public string Label { get; set; }
    ---Ignored for brevity---
    public virtual ICollection<ClassFee> ClassFees { get; set; }

}

 public class ClassFee
{
    public int Id { get; set; }
    [Display(Name ="Fee")]
    public int FeeId { get; set; }
    [Display(Name ="Class")]
    public int ClassId { get; set; }

    ---ignored for brevity---

    [ForeignKey("FeeId")]
    public Fee Fee { get; set; }

    [ForeignKey("ClassId")]
    public StudentClass Class { get; set; }
}

The following is my FeesController class where I intend to get the details of a given fee and the list of classes the fee is applied to以下是我的 FeesController 类,我打算在其中获取给定费用的详细信息以及费用适用的类列表

public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var fee = await _context.Fees
            .Include(cf => cf.ClassFees)
            .FirstOrDefaultAsync(m => m.Id == id);
        if (fee == null)
        {
            return NotFound();
        }

        return View(fee);
    }

I was expecting that with this, I should be able to have the class for the fees in the view by calling classFeeEntityInstance.class.label to get the label for the class but it returns null.我期望通过这个,我应该能够通过调用classFeeEntityInstance.class.label来获取视图中的费用类来获取类的标签,但它返回 null。 Also when I put a breakpoint on the method and run the code, the classfee.class is returned as null此外,当我在方法上放置断点并运行代码时,classfee.class 返回为 null

I have also tried to do the following to see if I could eagerly load the class in the query but it does not seen to be possible to find the class from the classfee with the ThenInclude caluse as follows我还尝试执行以下操作,以查看是否可以在查询中急切地加载该类,但似乎无法使用 ThenInclude caluse 从 classfee 中找到该类,如下所示

.Include(cf => cf.ClassFees).ThenInclude(f=>f.Class)

but the f.Class does not exist at that point because the intellisence in Visual Studio does not suggest it and it underlines it immediately I try to add it.但是 f.Class 那时不存在,因为 Visual Studio 中的智能不建议它并且它立即强调它我尝试添加它。

Below is how I wish to use the class under the classFees in my view以下是我希望如何在我看来使用 classFees 下的类

@foreach (ClassFee classFee in Model.ClassFees)
{
   @classFee.Class.Label
}

But the Class.Label throws up a null reference exception when the code is run但是当代码运行时 Class.Label 会抛出一个空引用异常

The application is being built on ASP.NET-Core 3.1 with Entity Framework 3.1该应用程序基于 ASP.NET-Core 3.1 和 Entity Framework 3.1

I will appreciate any guide to resolve this Thank you我将不胜感激任何解决此问题的指南 谢谢

Go ahead and try to build it using ThenInclude , it should work.继续尝试使用ThenInclude构建它,它应该可以工作。 There is a known issue with Intellisense and ThenInclude . Intellisense 和ThenInclude存在一个已知问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM