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