Entity Framework Database.SetInitializer simply not working

Viewed 31593

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

4 Answers
Related