New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

MP.EF6 + ASP.NET identity + Owin #98

Closed
Elvinra opened this Issue Mar 10, 2015 · 15 comments

Comments

Projects
None yet
5 participants
@Elvinra

Elvinra commented Mar 10, 2015

When i tryed to init MP.EF6 in my Startup class, i have an exception 馃憤

Message : The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

StackTrace :
  at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.AddLoadedHandler(EventHandler`1 handler)
  at System.Data.Entity.DbConfiguration.add_Loaded(EventHandler`1 value)
  at StackExchange.Profiling.EntityFramework6.MiniProfilerEF6.Initialize() dans d:\Files\GitHub\miniprofiler\dotnet\StackExchange.Profiling.EntityFramework6\MiniProfilerEF6.cs:ligne 30
  at ManahostManager.App_Start.WebApiApplication.Configuration(IAppBuilder app) dans c:\Users\Fabrice\Source\Repos\manahostmanager\ManahostManager\App_Start\WebApiApplication.cs:ligne 86

That exception was only occured when i migrate to ASP.NET Identity after that he is working fine

@Furynation

This comment has been minimized.

Furynation commented Sep 21, 2015

I am having this same issue after migrating to ASP.NET Identity. It seems the OWIN middleware does some EF configuring even before Application_Start is called? I've tried initializing in Application_Start, and in the ConfigAuth method of the OWIN startup class with no luck.

@ksitarek

This comment has been minimized.

ksitarek commented Oct 4, 2015

In my case it was related to Ninject, specifically making an instance of DbContext within RegisterServices. When I've turned it off, MiniProfilerEF6 have started.

Basically this error means that some other EF operations been made BEFORE calling MiniProfilerEF6.Initialize().

@NickCraver

This comment has been minimized.

Member

NickCraver commented Jun 10, 2016

Is this still an issue in current code? These aren't platforms we're using so I'm unaware of specific issues here.

@vishnu4

This comment has been minimized.

vishnu4 commented Nov 19, 2016

Yes this is still an issue. I'm getting this problem on an OWIN application

@NickCraver

This comment has been minimized.

Member

NickCraver commented Nov 30, 2016

I've honestly never used this stuff, does anyone have recommendations here? If it's a breaking change, v4 (#144) would be the time to make it...

@NickCraver

This comment has been minimized.

Member

NickCraver commented Mar 20, 2017

@chadwackerman I don't have a EF Core version for v4 yet...I'll take a look this week and see what's actually needed there. We don't use EF at Stack, so I'm not super familiar with it, will have to dig in. I'll see how we pass a profiled connection in there.

@NickCraver

This comment has been minimized.

Member

NickCraver commented Apr 11, 2017

@chadwackerman The v4 alpha is up, with an EntityFrameworkCore package as well: https://www.nuget.org/packages/MiniProfiler.EntityFrameworkCore/

@vishnu4

This comment has been minimized.

vishnu4 commented Apr 11, 2017

Just a note, I've never tried this with EntityFrameworkCore. This issue would happen for me with the .Net Framework 4.6 using EF6.

@NickCraver

This comment has been minimized.

Member

NickCraver commented Sep 4, 2017

@vishnu4 alpha8 is now up with latest support for both ASP.NET (and Core) and Entity Framework 6 (and Core), can you please tell me if these issues remain?

@vishnu4

This comment has been minimized.

vishnu4 commented Sep 4, 2017

Sorry, no luck. Same as before. I have the following in my packages.config:

<package id="MiniProfiler" version="4.0.0-alpha8-165" targetFramework="net461" />
  <package id="MiniProfiler.EF6" version="4.0.0-alpha8-165" targetFramework="net461" />
  <package id="MiniProfiler.Mvc5" version="4.0.0-alpha8-165" targetFramework="net461" />
  <package id="MiniProfiler.Shared" version="4.0.0-alpha8-165" targetFramework="net461" />

I went to your code at https://github.com/MiniProfiler/dotnet/blob/master/samples/Samples.Mvc5/Global.asax.cs and updated my files to match yours. If i load up my application, i get:

The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

which happens at the
MiniProfilerEF6.Initialize();
line. If i comment that out, the application runs fine w/out errors.

@NickCraver

This comment has been minimized.

Member

NickCraver commented Sep 6, 2017

@vishnu4 Does PreApplicationStartMethod appear anywhere in that project? Those would run even earlier, and our .Initialize() call would need to be moved there.

@vishnu4

This comment has been minimized.

vishnu4 commented Sep 6, 2017

that was it for me. I tried putting it as the last line in my PreApplicationStartMethod, and that still didn't work, but putting it as the first line did. All good after that, thank you.

@NickCraver

This comment has been minimized.

Member

NickCraver commented Sep 6, 2017

\o/ Woohoo I'm not crazy! Thanks for helping me figure that out @vishnu4 :)

@NickCraver

This comment has been minimized.

Member

NickCraver commented Sep 6, 2017

Closing this out, see above comments for the most likely cause of this behavior.

@NickCraver NickCraver closed this Sep 6, 2017

NickCraver added a commit that referenced this issue Sep 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment