【问题标题】:InvalidOperationException: The expression is invalid inside an 'Include' operation, since it does not represent a propertyInvalidOperationException:表达式在“包含”操作中无效,因为它不代表属性
【发布时间】:2022-01-01 14:34:38
【问题描述】:

我正在使用

  • Microsoft Visual Studio Community 2022(64 位)- 预览版 17.1.0 预览版 1.1
  • .NET Framework 版本 4.8.04084
  • ASP.NET Core 5,Razor 页面(非 MVC)
  • 实体框架 5

我在以下代码中收到“无效操作”异常,我不知道为什么。

简而言之,我有两个模型:

  1. 首字母缩略词
  2. AcronymOld

AcronymAcronymOld 具有一对多关系。

缩写:

public class Acronym
{
     [Key]
     public int Id { get; set; }

     [Required]
     [Display(Name = "Acronym")]
     [StringLength(50, ErrorMessage = "Acronym cannot be longer than 50 characters.")]
     public string Abbreviation { get; set; }

     [Required]
     [Display(Name = "Description")]
     [StringLength(100, ErrorMessage = "Description cannot be longer than 100 characters.")]
     public string Description { get; set; }

     [StringLength(250, ErrorMessage = "URL cannot be longer than 250 characters.")]
     public string URL { get; set; }

     public List<AcronymOld> AcronymsOld { get; set; }
}

AcronymOld:

public class AcronymOld
{
    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Previous Acronym")]
    [StringLength(50, ErrorMessage = "Previous Acronym cannot be longer than 50 characters.")]
    public string Abbreviation { get; set; }

    [Required]
    [Display(Name = "Previous Acronym Description")]
    [StringLength(100, ErrorMessage = "Previous Acronym Description cannot be longer than 100 characters.")]
    public string Description { get; set; }

    [Display(Name = "Last Used")]
    public DateTime LastUsed { get; set; }

    //Acronym Can Have One or More AcronymOld
    [ForeignKey("AcronymId")]
    public int AcronymId { get; set; }
    public Acronym Acronym { get; set; }
}

有问题的代码:

public class IndexModel : PageModel
{
    private readonly ApplicationDbContext _context;

    public IndexModel(ApplicationDbContext context)
    {
        _context = context;
    }

    public bool isDirty = false;

    public List<SelectListItem> Acronyms { get; set; }
    public IList<AcronymOld> AcronymOld { get; set; }

    [BindProperty(SupportsGet = true)]
    public string SearchString { get; set; }

    [BindProperty(SupportsGet = true)]
    public string SelectedAcronym { get; set; }

    public async Task OnGetAsync()
    {
        // Populate Acronym SelectList
        Acronyms = _context.Acronym.Select(a =>
                              new SelectListItem
                              {
                                  Value = a.Id.ToString(),
                                  Text = a.Abbreviation,
                              }).ToList();

        var acronymsOld = from m in _context.AcronymOld
                          select m;

        if (!string.IsNullOrEmpty(SelectedAcronym))
        {
            acronymsOld = acronymsOld.Where(m => m.Id == Int32.Parse(SelectedAcronym));
            isDirty = true;
        }

        if (!string.IsNullOrEmpty(SearchString))
        {
            if (isDirty == true)
            {
                // AcronymOld selected. Do not GroupBy
                acronymsOld = acronymsOld.Where(m => m.Abbreviation.Contains(SearchString));
            }
            else
            {
                //AcronymOld not selected. Use GroupBy
                acronymsOld = acronymsOld.Where(m => m.Abbreviation.Contains(SearchString));
            }
        }

        AcronymOld = await acronymsOld.Include(m => m.AcronymId)
            .OrderBy(m => m.Abbreviation)
            .AsNoTracking()
            .ToListAsync();
    }
}

我在这一行收到错误:

AcronymOld = await acronymsOld.Include(m => m.AcronymId)
            .OrderBy(m => m.Abbreviation)
            .AsNoTracking()
            .ToListAsync();

详细错误:

InvalidOperationException:表达式“m.AcronymId”在“包含”操作中无效,因为它不代表属性访问:“t => t.MyProperty”。要定位在派生类型上声明的导航,请使用强制转换 ('t => ((Derived)t).MyProperty') 或 'as' 运算符 ('t => (t as Derived).MyProperty')。可以通过组合 Where、OrderBy(Descending)、ThenBy(Descending) 来过滤集合导航访问

AcronymOld = 等待 acronymsOld.Include(m => m.AcronymId)

【问题讨论】:

  • 就是这样!非常感谢!!

标签: c# visual-studio asp.net-core entity-framework-core


【解决方案1】:

您需要在 .Include() 中使用 导航属性m.Acronym - 而不是 AcronymId 值) - 试试这个:

AcronymOld = await acronymsOld.Include(m => m.Acronym)
                              .OrderBy(m => m.Abbreviation)
                              .AsNoTracking()
                              .ToListAsync();

【讨论】:

    猜你喜欢
    • 2022-07-11
    • 2021-03-25
    • 1970-01-01
    • 2021-03-07
    • 2021-04-28
    • 2018-12-02
    • 2017-04-11
    相关资源
    最近更新 更多