Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Child Property in Entity Framework Core

I am working in .NET Core and Entity Framework Core.

I have the following model classes

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; }
}

In my FD controller I am trying to access properties of Bank and Branches.

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

But encountering error as

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'.'

Tried Configuring my DB Context as well but didnt work out.

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

GetAll in Repo is implemented as below

  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();
    }
like image 713
Bombo Avatar asked May 08 '26 16:05

Bombo


1 Answers

The issue seems to be that you are trying to include 'Bank', which is 2 levels deep.

The following should work:

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

EF Core also has the type-safe 'ThenInclude' construct, although it might not be a direct fit for your case.

query.Include(fd => fd.Branch)
    .ThenInclude(b => b.Bank);
like image 57
Ε Г И І И О Avatar answered May 11 '26 07:05

Ε Г И І И О