System.InvalidOperationException:'因警告'Microsoft.EntityFrameworkCore.Query.InvalidIncludePathError'生成了错误

问题描述 投票:0回答:1

我收到此错误:

System.InvalidOperationException:“因警告“Microsoft.EntityFrameworkCore.Query.InvalidIncludePathError”生成错误:无法找到在基于字符串的包含路径“ApplicationUser”中指定的导航“ApplicationUser”。通过将事件 ID“CoreEventId.InvalidIncludePathError”传递给“DbContext.OnConfiguring”或“AddDbContext”中的“ConfigureWarnings”方法,可以抑制或记录此异常。

来自我的存储库类(这是函数):

public T GetFirstOrDefault(Expression<Func<T, bool>> filter, string? includeProperties = null, bool tracked = true)
{
    if (tracked)
    {
        IQueryable<T> query = dbSet;

        query = query.Where(filter);

        if (includeProperties != null)
        {
            foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProp);
            }
        }

        return query.FirstOrDefault();
    }
    else
    {
        IQueryable<T> query = dbSet.AsNoTracking();

        query = query.Where(filter);

        if (includeProperties != null)
        {
            foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProp);
            }
        }

        return query.FirstOrDefault();
    }
}  

我哪里做错了?如果有人知道的话请帮我解决我的问题problem

c# asp.net entity-framework-core invalidoperationexception
1个回答
0
投票

您需要检查以下几点来修复给定的错误:

  • 检查您的代码中实体名称、属性名称或包含的拼写错误 路径可能会导致此类问题。
  • 确保您使用的是兼容且最新版本的 EF 您项目的核心。
  • 确保在 GetFirstOrDefault 中使用实体类型 T 方法有一个名为“ApplicationUser”的导航属性,如果您是 在 includeProperties 参数中指定它。
  • 检查您传递给 includeProperties 参数的值。 确保没有多余的空格、拼写错误或不正确的属性 逗号分隔字符串中的名称。应该和导航一致 实体 T 的属性名称。
  • 确保 Entity Framework Core 模型配置适合您的 实体 T 正确定义了导航属性。如果你是 使用属性验证导航属性是否正确 映射。
  • 检查与您的应用程序关联的 DbContext 类并进行 确保它具有所有相关实体的 DbSet 属性,包括 ApplicationUser(如果这就是您想要包含的内容)。
© www.soinside.com 2019 - 2024. All rights reserved.