Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize database with Entity Framework and Membership tables

Tags:

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?

like image 367
Patrick Desjardins Avatar asked Sep 30 '12 15:09

Patrick Desjardins


People also ask

Which of the following are database initialization strategies in EF Code First?

DropCreateDatabaseAlways: This strategy of code first approach creates the database every time you run the application. If the database already exists and there is no modification done in the model, it will drop the database and create a new one. In this approach, you will also lose the data.

What is the purpose of drop Create database always database initializer in Entity Framework?

DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not.


1 Answers

Take a look at the following article for the recommended approach for seeding your database using migrations.

Here are the steps:

  1. Create a new ASP.NET MVC 4 application using the Internet Template
  2. In your package manager console type the following command:

    enable-migrations 
  3. 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" });         }     } } 
  4. 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> 
  5. Run the migration in your package manager console:

    update-database -verbose 
like image 52
Darin Dimitrov Avatar answered Nov 08 '22 17:11

Darin Dimitrov