The application I am working on has several projects, including an "Infrastructure" (.Net Standard) where the dbContext is defined, a WinForms project (.Net Framework), and a ASP.Net web project (.Net Framework). The web project is only used for Web Services (asmx).
The issue I am experiencing is that whenever I call .Include()
on a IQueryable in order to include a collection navigation property from within the web project, I receive an ArgumentNullException
thrown by EntityFrameworkCore.
For example:
context.Reports.Include(e => e.ReportParamters).ToList();
Value cannot be null. Parameter name: frameworkName
at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrCollectionAccessorFactory.Create(INavigation navigation)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Navigation.<>c.<get_CollectionAccessor>b__21_0(Navigation n)
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Navigation.get_CollectionAccessor()
at Microsoft.EntityFrameworkCore.NavigationExtensions.GetCollectionAccessor(INavigation navigation)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.CustomShaperCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node)
at System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)
at System.Linq.Expressions.ConditionalExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node)
at System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression`1 node)
at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQueryExpression(ShapedQueryExpression shapedQueryExpression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at TRYADWebService.TRYADWebService.ItemWithNoLocationReport(String parameters) in C:\VisualStudioProjects\TRYAD\TRYADWebService\TRYADWebService.asmx.cs:line 435
I only get this error when the WinForms project calls a WebService method (which in turn calls code that includes the query). For development purposes the WinForms project references the web service using "localhost". The exact same EF query works when called directly from the WinForms project, and any query that does not use eager-loading works from the web project.
Not sure if this is relevant, but I recently converted the Infrastructure project from .Net Framework to .Net Standard and updated all the projects in the solution to use PackageReferences instead of package.config files. Unfortunately, it seems that ASP.Net web projects do not support PackageReferences, so I had to add all of the packages used in the other projects (including transitive dependencies) to the web project's package.config file. Here is the package.config file from the web project:
Web project's package.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.4.0" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.1" targetFramework="net48" />
<package id="Microsoft.Bcl.HashCode" version="1.1.1" targetFramework="net48" />
<package id="Microsoft.Data.SqlClient" version="1.1.3" targetFramework="net48" />
<package id="Microsoft.Data.SqlClient.SNI" version="1.1.0" targetFramework="net48" />
<package id="Microsoft.EntityFrameworkCore" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.EntityFrameworkCore.Abstractions" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.EntityFrameworkCore.Analyzers" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.EntityFrameworkCore.Proxies" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.EntityFrameworkCore.Relational" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.EntityFrameworkCore.SqlServer" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Caching.Abstractions" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Caching.Memory" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration.Binder" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.DependencyInjection" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Logging" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Options" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Extensions.Primitives" version="3.1.23" targetFramework="net48" />
<package id="Microsoft.Identity.Client" version="3.0.8" targetFramework="net48" />
<package id="Microsoft.IdentityModel.JsonWebTokens" version="5.5.0" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Logging" version="5.5.0" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Protocols" version="5.5.0" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="5.5.0" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Tokens" version="5.5.0" targetFramework="net48" />
<package id="Microsoft.Linq.Translations" version="2.0.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Collections.Immutable" version="1.7.1" targetFramework="net48" />
<package id="System.ComponentModel.Annotations" version="5.0.0" targetFramework="net48" />
<package id="System.Data.Common" version="4.3.0" targetFramework="net48" />
<package id="System.Diagnostics.DiagnosticSource" version="4.7.1" targetFramework="net48" />
<package id="System.IdentityModel.Tokens.Jwt" version="5.5.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.1" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
</packages>
update
If the DbContext is configured to allow lazy-loading and I don't use eager-loading, I don't get an exception, but the collection is empty. In the following example, the reports
variable is not empty, but the reportWithParams
variable is null. This is despite the EF query explicitly filtering to only reports with parameters, and lazy-loading being enabled.
var reports = context.Reports.Where(e => e.ReportParameters.Any()).ToList();
var reportWithParams = reports.FirstOrDefault(e => e.ReportParameters.Any());