3

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

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Updated my question with GetAll Code.. Cant we have some generic solution for this issue??
I also found that EF Core must not be trimming table names like they are in .Net 4. I have a habit of using a space after my commas for readability. My generic repo was failing because it could not find the child property with the space in front! Ugh. Just add Trim() function to includeProp var.
0

It`s because EF Core can find noway of applying an equivalent convention. you should check the relationship between entities .In my case I had a many to many relationship:

 public ICollection<BlogKeywords> Keywords { get; set; } = new List<BlogKeywords>();

changed to

   public ICollection<BlogKeywords> BlogKeywords { get; set; } = new List<BlogKeywords>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.