I'm using the latest MVC, Identity, EntityFramework, as well as the official Identity sample solution.
There are lots of ways to run a database initializer in the App_Start(), (e.g. (DropCreateDatabaseIfModelChanges, DropCreateDatabaseAlways).
I tried:
AppDbContext.Instance.Database.Initialize(true); // Instance is static builder Problem is that with Identity/OWIN, the seeding function pulls manager objects out of the OWIN context (via HttpContext.Current.GetOwinContext()), which apparently, doesn't exist that early in the lifecycle.
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 setup properly, and runs as expected. It's only if I try access it in App_Start that I get this problem.
Initializing the db in App_Start is not strictly necessary, but I prefer explicit code, and want various init routines in there, including creation/seeding of the db. How do I do that?
UserManagerand/orRoleManagerinstead?