Why is the Lambda Expression not valid in this Include()?

asp.net asp.net-mvc c# entity-framework-core

Question

I'm working with EFCore for the first time, after moving from EF6, and for some reason I keep getting this error:

System.InvalidOperationException: 'Lambda expression used inside Include is not valid.'

Here is my Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        using (var IJ = IJContext.CreateNew(false))
        {
            var ChargeList = iJ.Charges.Where(charge => charge.CourtCase.CaseNumber == "13457894561")
                                             .Include(charge => charge.ChargeDate)
                                             .ToList();
            return View(ChargeList);
        }
    }
}

Am I missing something important here? Does EFCore handle Lambdas totally differently or something?

1
1
1/10/2020 4:49:42 PM

Accepted Answer

It seems that the ChargeDate is not a related entity. Check Retaled entities documentation to see the purpose of include

Seems like you either have not a relationship between the types and that you want to use something like the select property to get new objects based on the data already retrieved from the query.

1
1/10/2020 4:44:24 PM

Popular Answer

If you look at the signature of the Include extension method it looks like as follows:

public static IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>( 
      this IQueryable<TEntity> source,
      Expression<Func<TEntity, TProperty>> navigationPropertyPath)
      where TEntity : class
{
       // Method body
}

The paramter navigationPropertyPath expecting a lambda expression representing the navigation property to eager load the data for that navigation property. But you are passing the entity property ChangeDate which is not navigation property.

For more details: EF Core Eager loading



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow