Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Database.SetInitializer simply not working

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

like image 247
geCoder Avatar asked Jun 16 '11 07:06

geCoder


People also ask

What is database Setinitializer?

The database initializer is called when a the given DbContext type is used to access a database for the first time. The default strategy for Code First contexts is an instance of CreateDatabaseIfNotExists<TContext>. C# Copy.

What is Entity Framework in MVC?

Entity Framework is an open-source ORM framework for . NET applications supported by Microsoft. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored.


1 Answers

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.

like image 53
woggles Avatar answered Oct 06 '22 07:10

woggles