如何停止添加,迁移检查使用$ C $基于C语言的迁移,当我的数据库没有未处理的迁移? [英] How can I stop Add-Migration checking my database has no pending migrations when using Code-Based migrations?

查看:99
本文介绍了如何停止添加,迁移检查使用$ C $基于C语言的迁移,当我的数据库没有未处理的迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用$ C $基于C语言的EF迁移的一个产品,它的调查不的使用EF。一般一切运作良好,除了命令:

I'm investigating using Code-Based EF Migrations for a product that does not use EF. Everything generally works well, except that the command:

Add-Migration MyTestMigration

输出以下消息:

outputs the following message:

无法产生明确的迁移,因为以下明确的迁移正在等待:[201206260845338_DannyTest]。试图产生一个新的明确的迁移之前应用挂起明确的迁移。

Unable to generate an explicit migration because the following explicit migrations are pending: [201206260845338_DannyTest]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

这样做的原因是,连接字符串是不知道在构建时,和EF已随机创建了一个名为的MyContextName数据库。\ SQLEx preSS。我不能应用挂起的迁移,因为它引用不存在此数据库中的数据库表 - 我们只是试图用迁移作为执行我们的脚本的一种方式;

The reason for this is that the connection string is not known at build time, and EF has randomly created a database called "MyContextName" on .\SQLExpress. I cannot apply the pending migration, because it references database tables that do not exist in this database - we're just trying to use migrations as a way of executing our scripts;

所以,问题是:

  1. 如果我们不使用自动迁移(我们有EnableAutomaticMigrations = FALSE),为什么添加迁移要求数据库是由最新的 尽管对产生的(空)的迁移绝对没有影响?我很难相信MS不打算使用此情况下,当这么多的工作原理;唯一的破的事情是确认不影响的任意的行为。

  1. If we're not using Automatic Migrations (we have EnableAutomaticMigrations=false), why does Add-Migration require that the database is up-to-date even though it has absolutely no impact on the generated (empty) migration? I find it hard to believe MS don't intend this use case when so much of it works; the only "broken" thing is validation that doesn't affect any behaviour.

有没有解决这个其他任何方式比创造我们只是复制了EF人做了什么自己的附加迁移命令,但跳过了(貌似不用)DB了最新的检查?我试图通过各种争论,但至今没有设法使它工作。

Is there any way around this other than creating our own Add-Migration command that just duplicates what the EF one does, but skips the (seemingly needless) DB up-to-date check? I've tried passing various arguments, but so far not managed to make it work.

我居然找到了一个更好的办法来解决这个问题,但它不是一个真正的回答这些问题,所以这里将其添加。希望能有时间把它变成一个博客文章

I actually found a better way to solve this problem, but it's not really an answer to these questions, so adding it here. Hopefully will get time to turn this into a blog post!

我想用的唯一原因加入迁移是因为所有参加随着DbMigration的废话;但我意识到,一个基类,我们可以基本消除这方面的需要所有具有基础类自动生成的属性迁移ID。目标是相同的所有的迁移,作为模型状态不会改变。现在,我们只需手动创建我们的迁移这样的(建ID,使得EF将应用它们以正确的顺序日期是必需的):

The only reason I wanted to use Add-Migration was because of all the guff that went along with the DbMigration; but I realised that with a base class, we could basically eliminate the need for all this by having the base class auto-generate the migration ID from an attribute. The Target is identical for all our migrations, as the model state doesn't change. Now, we just manually create our migrations like this (the date is required to build the ID such that EF will apply them in the correct order):

[Migration(2012, 6, 27, 12, 00, "Add new xxx fields for yyy")]
internal class MyNewMigration : MyDbMigration
{
    public override Up()
    {
        // ...
    }
    public override Down()
    {
        // ...
    }
}

MyDbMigration 类有目标/来源/ ID属性。目标是硬codeD(相同的值,添加迁移与第一迁移创建),来源为空,id是一些反思,读取MigrationAttribute。这意味着我们可以现在只需手动创建这些类;这是不是一个很大的努力,现在我们不担心所有IMigrationMetadata的东西: - )

The MyDbMigration class has the Target/Source/Id properties. Target is hard-coded (the same value that Add-Migration created with the first migration), Source is null, and Id is some reflection that reads the MigrationAttribute. This means we can now just manually create these classes; which isn't a lot of effort now we don't have to worry about all the IMigrationMetadata stuff :-)

推荐答案

尝试注释掉现有的迁移(即没有被应用到创建的数据库中的。\ SQLEx preSS),并重新运行您应用程序。应该填充本地数据库提供它所需要的初始表

Try commenting out your existing migrations (the ones that haven't been applied to the database created on .\SQLExpress) and re-running your app. That should populate the local database with the initial tables it needs.

在本地数据库具有正确的结构,你应该能够注释您的迁移,然后使用更新数据库,使当地一上来更新。然后你就可以添加一个新的迁移。

Once the local database has the correct structure you should be able to uncomment your migrations and then use update-database to bring the local one up to date. Then you'll be able to add a new migration.

还记得,有一个在更新数据库命令-connectionString参数,这样你就可以在特定的服务器/数据库针对您的迁移。

Also remember that there's a -connectionString parameter on the update-database command, so you can target your migrations at a specific server/db.

这篇关于如何停止添加,迁移检查使用$ C $基于C语言的迁移,当我的数据库没有未处理的迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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