I use the latest MVC, Identity, EntityFramework, as well as the official sample solution .
There are many ways to start the database initializer in App_Start() (for example, ( DropCreateDatabaseIfModelChanges , DropCreateDatabaseAlways ).
I tried:
AppDbContext.Instance.Database.Initialize(true); // Instance is static builder
The problem is that using Identity / OWIN, the seeding function pulls manager objects from the OWIN context (via HttpContext.Current.GetOwinContext() ), which apparently does not exist, which is an early stage of the life cycle.
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager>(); var roleManager = HttpContext.Current.GetOwinContext().Get<RoleManager>();
So, I get:
InvalidOperationException: No owin.Environment item was found in the context.
The OWIN context is configured correctly and works as expected. This is only if I try to access it in App_Start to get this problem.
Initializing db in App_Start not strictly necessary, but I prefer explicit code and want various init routines, including creating / fetching db, to be there. How to do it?
source share