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

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

问题描述

我有这种神奇的问题在这里。
我目前使用的实体框架4.1 code第一种方法与我的ASP.NET MVC 3应用程序时,它伟大的工作,直到昨天...

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这个简单的code()方法

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 不会产生任何的数据库,甚至更糟的是,它也没有甚至还试图从上下文表,以填补现有数据库...
我试图调试,但它似乎是应用程序只是跳过数据库初始化code ...

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

我找到了一个解决办法,就是用这条线code的

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

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

所以在这里,我只是迫使code反正创建DB ...

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

但事实上Database.SetInitializer不会工作实在是烦人......这伟大的工作之前,我不知道发生了什么。
我看着在Windows事件日志,SQL服务器日志...但无所不有。只是沉默。
但也许我只是在寻找放错了地方? :•

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的防爆preSS 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.

如果您已覆盖在你的初始种子的方法如下:

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

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

当您使用MyContext的实例种子code将只运行。

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

或者,它会稍后当您使用上下​​文创建。它可能看起来像它已经停止了工作,因为你删除了一些code,将使用在应用程序启动时的上下文。

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天全站免登陆