Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8600465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:37:23+00:00 2026-06-12T01:37:23+00:00

I have a MVC4 web application that use Entity Framework 5.0 Code First. In

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:37:24+00:00Added an answer on June 12, 2026 at 1:37 am

    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
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MVC4 application that is calling Amazon Web Services. The amazon service
I have a web application that uses .NET Framework 4, MVC 4, Ninject 3.
I have created a custom razor helper in my MVC4 Web application that I
I have an azure web application, running MVC 4. It uses the entity framework
I have a layered web application that I built with ASP.NET MVC 4, WebAPI
I have an MVC4 web application In setting it up on IIS7.5 I added
I am currently working on MVC4 SinglePage Application. I have a Web Api method
I have a c# mvc4 twitter bootstap appharbor web site. Currently I use Vimeo
I currently have a web application that is based upon a 3 Tier architecture.
I have a ASP.NET MVC 4 project - CodeFirst with Entity Framework - that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.