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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:14:30+00:00 2026-06-06T19:14:30+00:00

I use Entity Framework code-first. DataBaseName – I want to create a database in

  • 0

I use Entity Framework code-first.

DataBaseName – I want to create a database in SQL Server.

In web.config:

<configuration>
  <connectionStrings>
    <add name="DataContext" 
         providerName="System.Data.SqlClient" 
         connectionString="Data Source=USER\SQLEXPRESS;Initial Catalog=DataBaseName;Integrated Security=True;Pooling=False;User Instance = False" />

in global.asax:

 protected override void OnApplicationStarted()
 {
     base.OnApplicationStarted();

     Database.SetInitializer(new DataContextInitializer());
 }

DataContextInitializer:

namespace CodeFirstMembershipSharp
{
    public class DataContextInitializer : DropCreateDatabaseAlways<DataContext>
    {
        protected override void Seed(DataContext context)
        {
            MembershipCreateStatus Status;
            Membership.CreateUser("Demo", "123456", null, null, null, true, out Status);
            Roles.CreateRole("Admin");
            Roles.AddUserToRole("Demo", "Admin");
        }
    }
}

DataContext:

public class DataContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Bid>()
            .HasRequired(a => a.Auction)
            .WithMany()
            .HasForeignKey(x => x.AuctionId).WillCascadeOnDelete(false);

        modelBuilder.Entity<Bid>()
           .HasRequired(a => a.User)
           .WithMany()
           .HasForeignKey(x => x.UserId).WillCascadeOnDelete(false);
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<Auction> Auctions { get; set; }
    public DbSet<Page> Pages { get; set; }
    public DbSet<Bid> Bids { get; set; }
}

But the database is not created. What did I forget to write?

  • 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-06T19:14:32+00:00Added an answer on June 6, 2026 at 7:14 pm

    Database.SetInitializer sets the initializer but doesn’t run the initialization. After Database.SetInitializer you need to add:

    using (var context = new DataContext())
    {
        context.Database.Initialize(false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I successfully created my first Entity Framework Code first project using SQL Server CE.
How can I use the database view in entity framework code first,
I try to create database from entity framework code first follow with this tutorial
I would like to use Entity Framework Code first approach with SQLCE4 database. Everything
I'm trying to use the Entity Framework code first without an existing database. I
Here's sample model classes, which being use with Entity Framework Code First: public class
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I currently use a generic repository pattern with Entity Framework code first: public interface
I have a database that was created with the Entity Framework's code first approach.
I have a small MVC 3 app using Entity Framework Code First and use

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.