如何在不使用更新数据库的情况下生成启用了迁移的EF6数据库? [英] How do I generate an EF6 database with migrations enabled, without using update-database?

查看:105
本文介绍了如何在不使用更新数据库的情况下生成启用了迁移的EF6数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EF5中,我依靠可以使用 Database.CreateIfNotExists()

In EF5, I relied on the fact that I could recreate my database from the model using Database.CreateIfNotExists()

我将根据需要生成迁移,但不要将其检入源代码控制中(因为它们往往是开发周期的产物),然后每个开发人员都将根据需要从模型中删除并重新创建自己的数据库。

I would generate Migrations as needed, but never check them in to source control (since they tended to be artifacts of the development cycle) Each developer would then delete and recreate their own databases, from the model as needed.

然后,我们将通过比较代码的分支来生成迁移,并获取SQL,以应用于生产数据库或其他共享数据库。

Then, we would generate migrations by comparing branches of our code, and get the SQL, to apply to production or other shared databases.

该工作流似乎不再起作用,因为启用迁移后无法从头开始生成数据库,而无需先生成所有迁移然后调用update-database。由于调用add-migration会修改csproj文件,因此使我的脚本(使我们可以轻松切换分支)无法使用。

This workflow no longer seems to work, as there is no way to generate a database from scratch when migrations are enabled, without first generating all the migrations and then calling update-database. Since calling add-migration modifies the csproj file, this makes my scripts (which allow us to easily switch branches) unusable.


已启用迁移对于上下文 ApplicationDbContext,但数据库不存在或不包含任何映射表。使用迁移来创建数据库及其表,例如,通过从程序包管理器控制台中运行 Update-Database命令。

Migrations is enabled for context 'ApplicationDbContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.

是否有任何方法可以还原到EF5行为,其中Database.Create将创建数据库的当前版本?

Is there any way to revert to EF5 behavior where Database.Create will create the current version of the db?

推荐答案

CreateDatabaseIfNotExists初始化数据库,并且能够使用update-database并运行我的应用程序,如果尚不存在,它将创建数据库。

I was using CreateDatabaseIfNotExists to initialize the DB and was able to use update-database and run my application and it would create the DB if it didn't already exist. This seems to have broken with EF 6.0.x.

这很有用,但不是我所用的。

我该做什么

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
        using (MyContext temp = new MyContext())
        {
            temp.Database.Initialize(true);
        }

注意:MyContext是我使用的上下文,Configuration是配置文件

Note: MyContext is the context I use and Configuration is the Configuration file that is created when migrations are enabled.

我见过很多帖子,人们对此有疑问,但很少有描述解决方案的帖子。我不确定为什么这么多人,包括我自己,都错过了这一重大变化……(如果某处对此有描述,直到为时已晚,我才看到它。)

I have seen a lot of posts where people are having problems about this, but not many that describe a solution. I'm not sure why so many people, including myself, have missed this breaking change... (If there's a description about this somewhere, I never saw it until it was too late.)

编辑:

这是我添加到上下文类中的代码。我不像以前那样喜欢它,但是现在可以完成工作。

This is code that I added to my context class. I don't like it as much as how it used to work, but it gets the job done for now. See @Doug's comment to the OP about voting on CodePlex.

private static readonly Object syncObj = new Object();
public static bool InitializeDatabase()
{
    lock (syncObj)
    {
        using (var temp = new TbdContext())
        {
            ObjectContext oc = null;
            try
            {
                oc = temp.ObjectContext;
            }
            catch (Exception ex)
            {
                //Ignore error
                Console.WriteLine(ex);
            }

            if (oc != null && oc.DatabaseExists())
            {
                return true;
            }
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<TbdContext, Configuration>());
            try
            {
                temp.Database.Initialize(true);
                return true;
            }
            catch (DataException ex)
            {

        }
    }
}

然后在Global.asax.cs Application_Start()中执行以下操作:

Then in my Global.asax.cs Application_Start() I do this:

if (databaseInitialized == false)
    databaseInitialized = MyContext.InitializeDatabase();

编辑:

我已升级到EF 6.1,发现它不再起作用。( https://stackoverflow.com/a/22770517/2033294 )这是我做了什么来解决它:

I upgraded to EF 6.1 and found this doesn't work anymore.(https://stackoverflow.com/a/22770517/2033294) Here's what I did to fix it:

    private static readonly Object syncObj = new Object();
    public static bool InitializeDatabase()
    {
        lock (syncObj)
        {
            using (var temp = new MyContext())
            {
                if (temp.Database.Exists()) return true;

                var initializer = new MigrateDatabaseToLatestVersion<MyContext, Configuration>();
                Database.SetInitializer(initializer);
                try
                {
                    temp.Database.Initialize(true);
                    return true;
                }
                catch (Exception ex)
                {
                    //Handle Error in some way
                    return false;
                }
            }
        }
    }

这篇关于如何在不使用更新数据库的情况下生成启用了迁移的EF6数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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