NullReferenceException: Object reference not set to an instance of an object. Entity Framework Core 3.1

Michael Mastro II 51 Reputation points
2022-02-11T21:33:33.76+00:00

I have the above error showing in Visual Studio 2019. The project is built on a Asp.Net Core 3.1 and EF Core 3.1, and the version in my QA environment is not demonstrating the errors. The only changes I made were to add Authentication and Authorization to the controllers. I did not change the DatabaseContext model or the database. So I am not sure why I am continually getting the error.

Here is the error:

modelBuilder.Entity<ViewModelClass.Dtl.Models.FactionViewModels.FactionViewModel>(query => {
                query.Ignore( q => q.FactionNames );
                query.Ignore( q => q.ReputationLevels );
                query.Ignore( q => q.ReputationTypes );
                query.HasNoKey();
            } );


NullReferenceException: Object reference not set to an instance of an object.
Microsoft.EntityFrameworkCore.Diagnostics.CoreLoggerExtensions.RequiredAttributeOnCollection(IDiagnosticsLogger<Model> diagnostics, INavigation navigation)

Microsoft.EntityFrameworkCore.Diagnostics.CoreLoggerExtensions.RequiredAttributeOnCollection(IDiagnosticsLogger<Model> diagnostics, INavigation navigation)
Microsoft.EntityFrameworkCore.Metadata.Conventions.RequiredNavigationAttributeConvention.ProcessNavigationAdded(IConventionRelationshipBuilder relationshipBuilder, IConventionNavigation navigation, RequiredAttribute attribute, IConventionContext<IConventionNavigation> context)
Microsoft.EntityFrameworkCore.Metadata.Conventions.NavigationAttributeConventionBase<TAttribute>.ProcessNavigationAdded(IConventionRelationshipBuilder relationshipBuilder, IConventionNavigation navigation, IConventionContext<IConventionNavigation> context)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+ImmediateConventionScope.OnNavigationAdded(IConventionRelationshipBuilder relationshipBuilder, IConventionNavigation navigation)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+RunVisitor.VisitOnNavigationAdded(OnNavigationAddedNode node)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+OnNavigationAddedNode.Accept(ConventionVisitor visitor)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+ConventionVisitor.Visit(ConventionNode node)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+ConventionVisitor.VisitConventionScope(ConventionScope node)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+ConventionBatch.Run()
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+ConventionBatch.Dispose()
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher+ImmediateConventionScope.OnEntityTypeAdded(IConventionEntityTypeBuilder entityTypeBuilder)
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnEntityTypeAdded(IConventionEntityTypeBuilder entityTypeBuilder)
Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(EntityType entityType)
Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(Type type, ConfigurationSource configurationSource)
Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(ref TypeIdentity type, ConfigurationSource configurationSource, Nullable<bool> shouldBeOwned)
Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(Type type, ConfigurationSource configurationSource, Nullable<bool> shouldBeOwned)
Microsoft.EntityFrameworkCore.ModelBuilder.Entity<TEntity>()
Microsoft.EntityFrameworkCore.ModelBuilder.Entity<TEntity>(Action<EntityTypeBuilder<TEntity>> buildAction)
Lotro.EntityClass.Dal.Models.LotroModel.OnModelCreating(ModelBuilder modelBuilder) in LotroModel.cs
+
            modelBuilder.Entity<ViewModelClass.Dtl.Models.FactionViewModels.FactionViewModel>(query => {
Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder+<>c.<TryAddCoreServices>b__7_3(IServiceProvider p)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(ServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(ServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<T>(IServiceProvider provider)
Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
Microsoft.EntityFrameworkCore.DbContext.Set<TEntity>()
Lotro.WebAppMVC.Controllers.ServersController.Index() in ServersController.cs
+
        public IActionResult Index() => View( _db.Set<ServerViewModel>().FromSqlRaw( sql: "SELECT Server_ID AS ServerID, Server_Name AS ServerName FROM dbo.Server WHERE ActiveRecord = 1 ORDER BY Server_Name" ).ToList() );
lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Does anyone see a reason why this is now broken?

Thanks in advance.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
705 questions
{count} votes