开发者

Entity Framework Database.SetInitializer simply not working

开发者 https://www.devze.com 2023-03-13 13:58 出处:网络
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...

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; }

    publi开发者_StackOverflow中文版c 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


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.


Another way to create a database

Database.SetInitializer(new CreateDatabaseIfNotExists<MyContext>());

var context = new MyContext();
context.Database.Create();


In the Global.asax.cs

Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, DbMigrationConfig>());

where DbMigrationConfig is the Configuration class in the migrations folder which I renamed. Also make sure that you enable the automatic migrations in the constructor of the config


I had the same issue with my Code First definitions and narrowed it down to multiple DBInitializer defined in one class file. So the thumb rule to follow after checking the syntax and usage is to:

1) Have only one Initializer definition in one physical file
2) Define the DbContext and Initializer in separate files

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消