How can I stop Entity Framework 5 migrations adding dbo. into key names?

前端 未结 4 1112
情深已故
情深已故 2020-12-05 00:32

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 ch

相关标签:
4条回答
  • 2020-12-05 00:59

    Improving on bricelam's answer, I tried this on EF6. Made a few changes to keep the schema as part of table name and only remove it from the FK or PK name

    internal class MyCodeGenerator : CSharpMigrationCodeGenerator
    {
        protected override void Generate(AddForeignKeyOperation addForeignKeyOperation, IndentedTextWriter writer)
        {
            addForeignKeyOperation.Name = this.StripDbo(addForeignKeyOperation.Name, addForeignKeyOperation.DependentTable);
            addForeignKeyOperation.Name = this.StripDbo(addForeignKeyOperation.Name, addForeignKeyOperation.PrincipalTable);
            base.Generate(addForeignKeyOperation, writer);
        }
    
        protected override void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation, IndentedTextWriter writer)
        {
            addPrimaryKeyOperation.Name = StripDbo(addPrimaryKeyOperation.Name, addPrimaryKeyOperation.Table);
            base.Generate(addPrimaryKeyOperation, writer);
        }
    
        protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation, IndentedTextWriter writer)
        {
            dropForeignKeyOperation.Name = this.StripDbo(dropForeignKeyOperation.Name, dropForeignKeyOperation.DependentTable);
            dropForeignKeyOperation.Name = this.StripDbo(dropForeignKeyOperation.Name, dropForeignKeyOperation.PrincipalTable);
            base.Generate(dropForeignKeyOperation, writer);
        }
    
        protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation, IndentedTextWriter writer)
        {
            dropPrimaryKeyOperation.Name = StripDbo(dropPrimaryKeyOperation.Name, dropPrimaryKeyOperation.Table);
            base.Generate(dropPrimaryKeyOperation, writer);
        }
    
        private string StripDbo(string objectName, string tableName)
        {
            if (tableName.StartsWith("dbo."))
            {
                return objectName.Replace(tableName, tableName.Substring(4));
            }
    
            return objectName;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 01:00

    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;
        }
    }
    

    Then set it in your migrations configuration class:

    class Configuration : DbMigrationsConfiguration<MyContext>
    {
        public Configuration()
        {
            CodeGenerator = new MyCodeGenerator();
        }
    }
    
    0 讨论(0)
  • 2020-12-05 01:12

    For Automatic Migrations use this code:

    public class MyOwnMySqlMigrationSqlGenerator : MySqlMigrationSqlGenerator
    {
        protected override MigrationStatement Generate(AddForeignKeyOperation addForeignKeyOperation)
        {
            addForeignKeyOperation.PrincipalTable = addForeignKeyOperation.PrincipalTable.Replace("dbo.", "");
            addForeignKeyOperation.DependentTable = addForeignKeyOperation.DependentTable.Replace("dbo.", "");
            MigrationStatement ms = base.Generate(addForeignKeyOperation);
            return ms;
        }
    }
    

    And Set it on configuration:

    SetSqlGenerator("MySql.Data.MySqlClient", new MyOwnMySqlMigrationSqlGenerator());
    
    0 讨论(0)
  • 2020-12-05 01:22

    This is a fine answer, however, if you're just looking for a 'quick fix' approach, there's this as well EF Migrations DropForeignKey fails when key is in a base class

    Use the DropForeignKey overload which contains the parameters principalName and name -- which in this case means constraint name!

    0 讨论(0)
提交回复
热议问题