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 t
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:
enable-migrations
This will create a ~/Migrations/Configuration.cs
file in which you could seed your database:
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web.Security;
using WebMatrix.WebData;
internal sealed class Configuration : DbMigrationsConfiguration<MvcApplication1.Models.UsersContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(MvcApplication1.Models.UsersContext context)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
if (!Roles.RoleExists("Administrator"))
{
Roles.CreateRole("Administrator");
}
if (!WebSecurity.UserExists("john"))
{
WebSecurity.CreateUserAndAccount("john", "secret");
}
if (!Roles.GetRolesForUser("john").Contains("Administrator"))
{
Roles.AddUsersToRoles(new[] { "john" }, new[] { "Administrator" });
}
}
}
Specify the memebership and role providers in your web.config:
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear/>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear/>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
Run the migration in your package manager console:
update-database -verbose