Ef6: NullReferenceException on Code First Migrations when Glimpse is installed

Created on 10 Nov 2017  路  18Comments  路  Source: dotnet/ef6

Hi,

ref: https://github.com/rikrak/EF6_2_InitialisationError

I've just upgraded to EF6.2 and I receive a NullReferenceException on startup. I've done a bit of digging around and I think it's results from having Glimpse.EF6 installed.

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

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
closed-fixed regression type-bug

Most helpful comment

When is a 6.3 package going to be released? I'm still getting bug reports on this every few weeks. What do I tell them about something fixed exactly a year ago now?

Can we please release 6.3? Or even a 6.2.1 with just the fixes? Please?

cc @divega

All 18 comments

Looks like this might have been introduced by some of the changes made around connection managemenrt: https://github.com/aspnet/EntityFramework6/commit/fad188c4f357af1de9ffbe0d26b28c0d82391545#diff-a45479295396de842101deaaf8b212dd
https://github.com/aspnet/EntityFramework6/commit/8e1e169ddaf9bf20e0389463486c092943854ccf#diff-a45479295396de842101deaaf8b212dd

Assigning this to @AndriySvyryd for further investigation.

Confirmed that this is a regression in 6.2

As a workaround you can supply the connection to the context:
C# public SampleContext() : base(new GlimpseDbConnection(new SqlConnection("Data Source=...")), contextOwnsConnection: false) { ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += ObjectContextOnObjectMaterialized; }

Alternatively Glimpse could modify their code so GlimpseDbConnection.State returns Closed or Broken when the connection has been disposed.

Is it worth me reporting this on the Glimpse project too?

Clearing milestone to consider patch.

@rikrak Yes, please do report this to Glimpse.

Notes from triage: as of now, we are planning to put this in 6.3.0 and not into a patch. This is because:

  • There is a workaround
  • It only seems to impact people using Glimpse when Migrator is being used, which we don't think is super common.

However, we will reconsider this if enough people are hitting the issue; so please comment if this is impacting you.

@rikrak When you have filed an issue with Glimpse, can you post a link here?

+1 - just hit this. I am downgrading to 6.1.3 for now.

Looks like SQLite people are also affected - see this StackOverflow post Updating to EF 6.2.0 from EF 6.1.3 causes cannot access a disposed object error

I have the same issue, and I don't use Glimpse. https://stackoverflow.com/q/47329496/1079908

@ferittuncer Seems that SQLite has the same behavior as Glimpse for disposed connections

Still an issue in latest 1.8.6 of glimpse, and EF6 6.2.0. I'm confused, why is Glimpse trying to slipstream itself into a database migration update operation when it need not store data in an EF6 controlled database?
This is without the Glimpse ADO module installed even. Just pure glimpse and AspNet.

very frustrating.

To add to the pile here, I just realized this is the cause of bug reports against MiniProfiler as well: https://github.com/MiniProfiler/dotnet/issues/243

There is no way to release this fix with a minor release?
I'm using Sqlite with Migrations and it automatically calls the Context constructor without pass a connection, giving me the "disposed error".
I'm needing to update to EF 6.2 but I'm getting this error. And EF 6.3 is too far to be released!

When is a 6.3 package going to be released? I'm still getting bug reports on this every few weeks. What do I tell them about something fixed exactly a year ago now?

Can we please release 6.3? Or even a 6.2.1 with just the fixes? Please?

cc @divega

Maybe just fork the repo, they won't do it.

@NickCraver sorry I didn't respond before, but I missed this mention. EF 6.3 will align with the .NET Core 3.0 RTM date, which isn't decided yet. I would expect us to start having preview builds of EF 6.3 in coming months. We'll update https://github.com/aspnet/EntityFramework6/issues/271 when we have more information.

6.3 has now been released for a while and I can confirm the issue is fixed!

Was this page helpful?
0 / 5 - 0 ratings