I have a MVC4 web application that use Entity Framework 5.0 Code First.
In Global.asax.cs I have a bootstrapper that initialize the Entity.Database, force the database to be initialized and initialize the database for the Membership. The code is this one:
System.Data.Entity.Database.SetInitializer(new DatabaseContextInitializer());
Database.Initialize(true);
WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, "UserProfile", "UserId", "UserName", autoCreateTables: true);
The DatabaseContextInitializer is very simple for the moment:
public class DatabaseContextInitializer : DropCreateDatabaseIfModelChanges<DatabaseContext>
{
protected override void Seed(DatabaseContext dbContext)
{
base.Seed(dbContext);
db.Set<Workout>().Add(new Workout {Id = 1, Name = "My First workout user1"})
}
}
The problem is that I cannot create User to the membership with:
WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, "UserProfile", "UserId", "UserName", autoCreateTables: true);
Because I have a problem with that the database is not created. How do you initialize some default user for your database with Entity Framework 5.0 and Asp.Net MVC 4?
Take a look at the following article for the recommended approach for seeding your database using migrations.
Here are the steps:
In your package manager console type the following command:
This will create a
~/Migrations/Configuration.cs
file in which you could seed your database:Specify the memebership and role providers in your web.config:
Run the migration in your package manager console: