c# - 在 Entity Framework Core 中包含子属性

标签 c# entity-framework-core

我在 .NET Core 和 Entity Framework Core 工作。

我有以下模型类

public class FD
{
  public virtual Branch Branch { get; set; }
  public int Id { get; set; }
  public int BranchId { get; set; }
}

public class Branch
{
    public Branch()
    {
        FD= new HashSet<FD>();
    }
    
    public virtual ICollection<FixedDeposit> FixedDeposits { get; set; }
    public virtual Bank Bank { get; set; }
}

public class Bank
{
    public Bank()
    {
       Branches = new HashSet<Branch>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Branch> Branches { get; set; }
}

在我的FD Controller 中,我正在尝试访问银行和分行的属性。

   public IActionResult Index()
    {
        ICollection<FD> fixedDeposits = _unitOfWork.FD.GetAll(includeProperties: "Branch,Bank").ToList();
        return View(fixedDeposits);
    }

但遇到错误为

System.InvalidOperationException: 'An error was generated for warning 'Microsoft.EntityFrameworkCore.Query.InvalidIncludePathError': Unable to find navigation 'Bank' specified in string based include path 'Bank'. This exception can be suppressed or logged by passing event ID 'CoreEventId.InvalidIncludePathError' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.'

也尝试配置我的数据库上下文,但没有成功。

optionsBuilder
          .UseLazyLoadingProxies()
          .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.InvalidIncludePathError))
          .UseSqlServer();

Repo中的GetAll实现如下

  public IEnumerable<T> GetAll(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = null)
    {
        IQueryable<T> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

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

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        return query.ToList();
    }

最佳答案

问题似乎是您试图包含 2 层深的“Bank”。

以下应该有效:

public IActionResult Index()
{
    ICollection<FD> fixedDeposits = _unitOfWork.FD.GetAll(includeProperties: "Branch,Branch.Bank").ToList();
    return View(fixedDeposits);
}

EF Core 还具有类型安全的“ThenInclude”构造,尽管它可能不直接适合您的情况。

query.Include(fd => fd.Branch)
    .ThenInclude(b => b.Bank);

关于c# - 在 Entity Framework Core 中包含子属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65511167/

相关文章:

c# - 如何将App.XAML样式应用到当前窗口

c# - 当两个实体可以独立存在时, Entity Framework 核心一对零或一

c# - 如何在 C# 中编辑具有多个引用的快捷方式的目标路径

c# - EF 4.1 代码首先初始化我的数据库

c# - ThenInclude 在 EF Core 查询中无法识别

c# - 如果始终在本地评估 Skip 和 Take,我如何在 EF Core 中运行分页查询?

c# - 在 Entity Framework Core 支持中始终加密

c# - Entity Framework Core - 派生类的多个鉴别器

c# - Unity - 在检查器中自定义结构图

c# - MS 图表控件 : Annotations do not display when using FastLines