本文介绍了如何停止添加dbo的Entity Framework 5迁移。成名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4.3代码首先启动了一个使用手动迁移和SQL Express 2008的项目,最近更新到了EF5(在VS 2010中),并注意到现在,当我更改类似外键约束时,迁移代码会添加 DBO。到表名的开始,因此它构造的外键名称对于现有的约束是不正确的(并且通常现在看起来很奇怪地命名)。

I started a project using Entity Framework 4.3 Code First with manual migrations and SQL Express 2008 and recently updated to EF5 (in VS 2010) and noticed that now when I change something like a foreign key constraint, the migrations code adds the "dbo." to the start of the table name and hence the foreign key name it constructs is incorrect for existing constraints (and in general now seem oddly named).

EF中的原始迁移脚本4.3(注意 ForeignKey(Products,t => t.Product_Id)):

Original migration script in EF 4.3 (note ForeignKey("Products", t => t.Product_Id)):

    CreateTable(
        "Products",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                ProductName = c.String(),
            })
        .PrimaryKey(t => t.Id);

    CreateTable(
        "KitComponents",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                Component_Id = c.Int(),
                Product_Id = c.Int(),
            })
        .PrimaryKey(t => t.Id)
        .ForeignKey("Products", t => t.Component_Id)
        .ForeignKey("Products", t => t.Product_Id)
        .Index(t => t.Component_Id)
        .Index(t => t.Product_Id);

生成的外键名称:
FK_KitComponents_Products_Product_Id
FK_KitComponents_Products_Component_Id

Foreign Key names generated:FK_KitComponents_Products_Product_IdFK_KitComponents_Products_Component_Id

如果我升级到EF5并更改外键,迁移代码看起来像(注意dbo.KitComponents和dbo.Products,而不是只需KitComponents和Products):

If I then upgrade to EF5 and change the foreign key the migration code looks something like (note the "dbo.KitComponents" and "dbo.Products" as opposed to just "KitComponents" and "Products"):

DropForeignKey("dbo.KitComponents", "Product_Id", "dbo.Products");
DropIndex("dbo.KitComponents", new[] { "Product_Id" });

,更新数据库失败,并显示消息:
' FK_dbo.KitComponents_dbo .Products_Product_Id '不是约束。
无法删除约束。看到以前的错误。

and the update-database fails with the message:'FK_dbo.KitComponents_dbo.Products_Product_Id' is not a constraint.Could not drop constraint. See previous errors.

所以看起来像EF5约束命名从
更改FK_KitComponents_Products_Product_Id

FK_dbo.KitComponents_dbo.Products_Product_Id (使用dbo。前缀)

so it seems as of EF5 the constraint naming has changed fromFK_KitComponents_Products_Product_IdtoFK_dbo.KitComponents_dbo.Products_Product_Id (with dbo. prefix)

如何让EF5像在EF 4.3中一样运行,所以我不必改变每一个新的迁移代码出来?

How can I get EF5 to behave as it was in EF 4.3 so I don't have to alter every piece of new migration code it spits out?

我无法找到有关为什么会发生变化的任何发行说明,以及如何处理:(

I haven't been able to find any release notes about why this changed and what to do about it :(

推荐答案

您可以通过对类:

You can customize the generated code by sub-classing the CSharpMigrationCodeGenerator class:

class MyCodeGenerator : CSharpMigrationCodeGenerator
{
    protected override void Generate(
        DropIndexOperation dropIndexOperation, IndentedTextWriter writer)
    {
        dropIndexOperation.Table = StripDbo(dropIndexOperation.Table);

        base.Generate(dropIndexOperation, writer);
    }

    // TODO: Override other Generate overloads that involve table names

    private string StripDbo(string table)
    {
        if (table.StartsWith("dbo."))
        {
            return table.Substring(4);
        }

        return table;
    }
}

然后将其设置在迁移配置类中: p>

Then set it in your migrations configuration class:

class Configuration : DbMigrationsConfiguration<MyContext>
{
    public Configuration()
    {
        CodeGenerator = new MyCodeGenerator();
    }
}

这篇关于如何停止添加dbo的Entity Framework 5迁移。成名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 18:38