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 6139349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:58:44+00:00 2026-05-23T17:58:44+00:00

I’m trying to get Entity Framework Code First to create a database for me

  • 0

I’m trying to get Entity Framework Code First to create a database for me but seem to be running into issues with it not actually creating the tables.

In my Web.Config I have the connection string setup and pointed to the database (without tables) I setup:

    <connectionStrings>
    <add name="DataContext" connectionString="Server=.\SQLEXPRESS;Database=MyDatabase;Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />
</connectionStrings>

In my DataConext.cs class located in my Domain project I have the following: (I am using the Altairis.Web.Security plugin to help with membership stuff)

public class DataContext : DbContext
{
    public DbSet<Faq> Faqs { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Maps to the expected many-to-many join table name for roles to users.
        modelBuilder.Entity<User>()
       .HasMany(u => u.Roles)
       .WithMany(r => r.Users)
       .Map(m =>
       {
           m.ToTable("RoleMemberships");
           m.MapLeftKey("UserName");
           m.MapRightKey("RoleName");
       });
    }
}

public class DataContextInitializer : CreateDatabaseIfNotExists<DataContext>
{
    protected override void Seed(DataContext context)
    {
        // create roles
        var roles = new List<Role>{
            new Role{RoleName = "Administrator"},   // admin area: admin user
            new Role{RoleName = "User"},            // admin area: normal user               
            new Role{RoleName = "Client"}           // front end: normal user
        };

        roles.ForEach(r => context.Roles.Add(r));
    }
}

In my Web project Global.asax.cs I have:

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        Database.SetInitializer(new DataContextInitializer());
    }

Everything compiles and works away but the database tables are never generated for me. From reading around online I think that this could be down to Database.SetInitializer but I’m not really sure what it should be adjusted to?

Any help to point me in the right direction would be great! I had this working on my home computer but running this project on another PC just isn’t working for some reason – typical 😉

Thanks,
Rich

  • 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-05-23T17:58:44+00:00Added an answer on May 23, 2026 at 5:58 pm

    Ok, got this working finally. Thanks to Ladislav Mrnka for the link I altered my global.asax to be:

    protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
    
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
    
    
            var initr = new DataContextInitializer();
            Database.SetInitializer<DataContext>(new DataContextInitializer());
            using (var db = new DataContext())
            {
                db.Database.CreateIfNotExists();
            }
    
            using (var db1 = new DataContext())
            {
                initr.Seed(db1);
            }
        }
    

    i also adjusted my DataContextInitializer using BumbleB2na’s suggestion. The finished class looks like this:

    public class DataContextInitializer : CreateDatabaseIfNotExists<DataContext>
    {
        public void Seed(DataContext context)
        {
            // create roles
            var roles = new List<Role>{
                new Role{RoleName = "Administrator"},   // admin area: admin user
                new Role{RoleName = "User"},            // admin area: normal user               
                new Role{RoleName = "Client"}           // front end: normal user
            };
    
            roles.ForEach(r => context.Roles.Add(r));
            context.SaveChanges();
        }
    }
    

    It now works perfectly if I leave Entity Framework create the database for me and not Create one myself before running the code. Hopefully this helps someone else!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.