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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:19:03+00:00 2026-05-23T04:19:03+00:00

I am having this kind of mysterious issue here. I am currently using Entity

  • 0

I am having this kind of “mysterious” issue here.
I am currently using Entity Framework 4.1 Code First approach with my ASP.NET MVC 3 application, it worked great, until yesterday…

Something really bad happened that caused my Database.SetInitializer to stop working.
Explained:

I have this simple model

public class User
{
    public int Id { get; set; }
    public int RoleId { get; set; }

    [Required]
    [StringLength(50)]
    [DataType(DataType.Text)]
    public string Login { get; set; }

    [Required]
    [StringLength(150)]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [StringLength(32)]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime RegisteredDate { get; set; }

    public virtual Role Role { get; set; }
}

And here is my DbContext

public class MyContext : DbContext
{
    public DbSet<Models.User> Users { get; set; }
}

Also I setup custom initializer (for test)

public class MyInitializer 
            : DropCreateDatabaseIfModelChanges<MyContext>
{
}

Now I already setup connection string in Web.config (with the name same as MyContext)
So in Global.asax I am calling this simple code in Application_Start() method

Database.SetInitializer(new MyInitializer());

But the problem is that Database.SetInitializer WONT create any database, and even worse, it also not even trying to FILL existing database with tables from context…
I tried to debug, but it seems like application just jumping over the database initializing code…

I found one solution, is to use this line of code

var ctx = new MyContext();
ctx.Database.Initialize(true);

So here I am just forcing code to create DB anyway…

But the fact Database.SetInitializer wont work is really annoying… it worked great before, I don’t know what happened.
I looked in windows events journal, sql server logs… but nothing is there. Just silence.
But maybe Im just looking in wrong place? :S

Can anybody tell me what is going on?

P.S I am using Visual Web Developer 2010 + SQL Server Express 2008 R2

  • 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-23T04:19:03+00:00Added an answer on May 23, 2026 at 4:19 am

    The database will only be created when you actually use the context.

    If you have overridden the Seed method in your initializer as follows:

    protected override void Seed(MyContext context){...}
    

    The Seed code will only run when you use an instance of MyContext.

    That is why it works when you use

    var ctx = new MyContext();
    ctx.Database.Initialize(true);
    

    You could always force it to create by using your context in the Application_Start() method in Global.asax.cs like:

            System.Data.Entity.Database.SetInitializer(new MyInitializer());
    
            MyContext db = new MyContext();
            db.Database.Initialize(true);
            //or even something like db.Users.Count();
    

    Or it will be created later on when you use your context. It might have looked like it had stopped working because you removed some code that would use the context on application startup.

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

Sidebar

Related Questions

Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
I'm having a problem here.. Supposed I have this kind of datetime. $date =
I'm having this very weird where i'm using rmtree(C:\myfolder); and it's throwing some kind
I'm having this argument about using Cursors in TSQL recently... First of all, I'm
I am currently using this regex: /([-0-9]+).([-0-9]+).([-0-9]+).([-0-9]+).(.*?).([-0-9]).([-0-9]+).([-0-9.]+).([-0-9.]+).([-0-9.]+).*/i to match this kind of thing: 1
When declaring a template, I am used to having this kind of code: template
Having this errors when trying to compile/run my app using Maven. I've succeeded to
Having this code... var b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
Having this code: $main = !empty($searchResults['main']) ? $searchResults['main'] : null; $second = !empty($searchResults['second']) ?
I have this idea of having some kind of badges in my app. They

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.