"There is already an open DataReader..." error when using the Database.SetInitializer in the Global.Asax ApplciaitonStart
I am wondering if anyone else has had an issue with running the DB initializer from the global asax?
I have this in the ApplicationStart:
Database.SetInitializer(new MyInitializer());
That runs fine开发者_开发技巧, after that once my application has started I try a login Method i created in my services. It fails when it tries to open the context.
My test application is setup almost the same way and doesn't have an issue.
Any thoughts?
Update: I tried adding the MultipleActiveResultSets=True and now I am getting this error: The underlying provider failed on Open.
Update 2: Well, it turns out that my application loads while the initializer is still finishing. That is why I was getting those errors. So, what I figured out is that part of the app loads and then it must request something from the DB (at which point it created the DB and seeds it). At that point part of the application has loaded, but you don't know that the initializer is still running.
Like I sad in my updates, the DB wasn't getting created until after most of the application had run already. It was still running even though, as a user, you wouldn't know it. So I created an initialization project and added this class:
public static class InitializeAndSeed
{
public static void Initialize()
{
Database.SetInitializer(new MyContextInitializer());
using (var db = new MyContext())
{
db.Database.Initialize(false);
}
}
}
In my Applicaiton_Start() I call the InitializeAndSeed.Initialize(). Worked perfectly.
This article helped me figure that out.
精彩评论