实体框架 Database.SetInitializer 根本不起作用 [英] Entity Framework Database.SetInitializer simply not working

查看:37
本文介绍了实体框架 Database.SetInitializer 根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了这种神秘"的问题.我目前在我的 ASP.NET MVC 3 应用程序中使用 Entity Framework 4.1 Code First 方法,它运行良好,直到昨天......

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...

发生了一些非常糟糕的事情,导致我的 Database.SetInitializer 停止工作.解释:

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

我有这个简单的模型

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

这是我的 DbContext

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

现在我已经在 Web.config 中设置了连接字符串(名称与 MyContext 相同)所以在 Global.asax 我在 Application_Start() 方法中调用这个简单的代码

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());

但问题是 Database.SetInitializer 不会创建任何数据库,更糟糕的是,它甚至不会尝试用上下文中的表填充现有数据库......我试图调试,但似乎应用程序只是跳过了数据库初始化代码...

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...

但是 Database.SetInitializer 无法工作的事实真的很烦人...以前它工作得很好,我不知道发生了什么.我查看了 windows 事件日志、sql server 日志……但什么也没有.只是沉默.但也许我只是找错了地方?:S

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 我使用的是 Visual Web Developer 2010 + SQL Server Express 2008 R2

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.

如果您在初始化程序中重写了 Seed 方法,如下所示:

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

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

种子代码只会在您使用 MyContext 的实例时运行.

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

您始终可以通过在 Global.asax.cs 中的 Application_Start() 方法中使用您的上下文来强制它创建,例如:

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.

这篇关于实体框架 Database.SetInitializer 根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆