Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

NullReferenceException with Entity Framework 6.2 #1007

Open
rikrak opened this issue Nov 23, 2017 · 2 comments
Open

NullReferenceException with Entity Framework 6.2 #1007

rikrak opened this issue Nov 23, 2017 · 2 comments

Comments

@rikrak
Copy link

rikrak commented Nov 23, 2017

Hi,

This is a cross post from the EF project
ref: dotnet/ef6#398
ref: https://github.com/rikrak/EF6_2_InitialisationError

Glimpse.EF6 does not appear to work with the recently release Entity Framework 6.2. On startup I receive a NullReferenceException.

The exception appears to come from a call to DbMigrator.GetPendingMigrations().
After a bit of debugging, it looks like a DbConnection is disposed of, which causes the GlimpseDbConnection to set the InnerConnection to null. I think another call is then made to the same connection after disposal, which then causes a NullReferenceException.

The same code worked with EF 6.1.3 and Glimpse.EF6

There is a proposed workaround on the EF issue, and there is also a suggested fix for the Glimpse code.

source Error:

Line 46:             var migrator = new DbMigrator(_configuration);
Line 47: 
Line 48:             if (migrator.GetPendingMigrations().Any())
Line 49:             {
Line 50:                 migrator.Update();

Source File: D:\Dev\Src\EF6_2_InitialisationError\UI.Web\UnitOfWork\DbInitialiser.cs Line: 48

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Glimpse.Ado.AlternateType.GlimpseDbConnection.get_State() +6
   System.Data.Entity.Internal.RepositoryBase.CreateConnection() +25
   System.Data.Entity.Migrations.History.HistoryRepository.QueryExists(String contextKey) +82
   System.Data.Entity.Migrations.History.HistoryRepository.Exists(String contextKey) +39
   System.Data.Entity.Migrations.History.HistoryRepository.GetPendingMigrations(IEnumerable`1 localMigrations) +46
   System.Data.Entity.Migrations.DbMigrator.GetPendingMigrations() +29
   UI.Web.UnitOfWork.CreateAndMigrateDatabaseInitializer`2.System.Data.Entity.IDatabaseInitializer<TContext>.InitializeDatabase(TContext context) in D:\Dev\Src\EF6_2_InitialisationError\UI.Web\UnitOfWork\DbInitialiser.cs:48
   System.Data.Entity.Internal.<>c__DisplayClassf`1.<CreateInitializationAction>b__e() +76
   System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +60
   System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +357
   System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +7
   System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +111
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +198
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73
   System.Data.Entity.Internal.InternalContext.Initialize() +30
   System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes() +48
   System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext() +25
   UI.Web.UnitOfWork.SampleContext..ctor() in D:\Dev\Src\EF6_2_InitialisationError\UI.Web\UnitOfWork\SampleContext.cs:13
   UI.Web.MvcApplication.Application_Start() in D:\Dev\Src\EF6_2_InitialisationError\UI.Web\Global.asax.cs:23

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +477
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +350
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +678
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +90
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +188
@RudeySH
Copy link

RudeySH commented Dec 2, 2019

FYI, the issue was caused by the database initializer and it's fixed in Entity Framework 6.3.

@jrmoreno1
Copy link

I am having a similar (same?) problem, with EF6.2 and EF6.4. The statck trace is different, but it's throwing from the same property.

Glimpse.Ado.AlternateType.GlimpseDbConnection.get_State() +3 System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch(TTarget target, Func3 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed) +103 System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.GetState(DbConnection connection, DbInterceptionContext interceptionContext) +502 System.Data.Entity.Core.EntityClient.EntityConnection.StoreCloseHelper() +285 System.Data.Entity.Core.EntityClient.EntityConnection.Dispose(Boolean disposing) +65 System.ComponentModel.Component.Dispose() +26 System.Data.Entity.Internal.LazyInternalConnection.Dispose() +51 System.Data.Entity.Internal.LazyInternalContext.DisposeContext(Boolean disposing) +96 System.Data.Entity.DbContext.Dispose(Boolean disposing) +27 System.Data.Entity.DbContext.Dispose() +25

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants