【问题标题】:Strategy for Getting SQL for AutomaticMigrations获取用于自动迁移的 SQL 的策略
【发布时间】:2012-07-01 18:53:45
【问题描述】:

我们使用的是 EF 5RC,代码先进行迁移。我觉得这应该是一个容易回答的问题(我希望)。有什么好方法可以弄清楚自动迁移正在尝试做什么。

我已通过 Add-Migration PS 命令添加了迁移。 我已经调用了 Update-Database 并且该迁移似乎一切正常。 现在 - 我只是像往常一样运行 Update-Database,但出现以下错误:

PM> update-database -Verbose
Using StartUp project 'Web'.
Using NuGet project 'DataAccess'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'UserGroup' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).
No pending code-based migrations.
Applying automatic migration: 201206301526422_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.

注意,我正在添加 -Verbose 选项,并且我已使用 -Script 再次尝试过。但我不知道我们要迁移到什么地方;以及什么 SQL - 或它认为会导致数据丢失的内容。

我不想在这里简单地启用“允许数据丢失”,而是想了解如何解决这些迁移问题。

提前谢谢你!

【问题讨论】:

    标签: entity-framework ef-code-first code-first entity-framework-migrations


    【解决方案1】:

    只要运行:

    PM> Update-Database -Script -Force
    

    这将生成 SQL 并将其显示在窗口中而不运行它。

    【讨论】:

    • 如果我启用允许数据丢失,它可以清除我所有的行吗?我将MaxLength 属性添加到一列,我知道数据库中的所有行实际上都没有超过该长度,如果我不介意被截断的话。所以这就是数据丢失的意思,或者它会从数据库中删除所有行?
    • @Shimmy 它不会删除你的行。
    • 但它可以删除或缩短列
    【解决方案2】:

    发布后我在 Azure 上遇到了这个错误,但是你不能使用 -Force ,所以全局解决方案(并且本地也不需要 -Force)

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true; // <-- THIS LINE
    }
    

    【讨论】:

    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 2013-04-21
    • 2011-04-18
    • 2010-09-18
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多