EF 4.3.1 Migration Exception - AlterColumn defaultValueSql creates same default constraint name for different tables

后端 未结 3 1755
再見小時候
再見小時候 2020-12-06 14:03

I have a DB that I created using the OOB database initializer, and I am using Code First with EF 4.3.1.

I wanted to take advantage of the new \"IgnoreChanges\" flag

相关标签:
3条回答
  • 2020-12-06 14:28

    It seems it's a known bug: msdn forums

    Andrew J Peters Microsoft (MSFT) replied:

    Thanks for reporting this. The issue will be fixed for RTM.

    A possible workaround is to initially make the column nullable, which will prevent Migrations from generating the extra DEFAULT constraint. Once the column is created, then it can be altered back to non-nullable.

    But it's definitelly not fixed in EF 4.3.1. Here is relevant part of the source:

    // Type: System.Data.Entity.Migrations.Sql.SqlServerMigrationSqlGenerator
    // Assembly: EntityFramework, Version=4.3.1.0, 
    // Culture=neutral, PublicKeyToken=b77a5c561934e089
    namespace System.Data.Entity.Migrations.Sql
    {
      public class SqlServerMigrationSqlGenerator : MigrationSqlGenerator
      {
         protected virtual void Generate(AlterColumnOperation alterColumnOperation)
         {
          //...
          writer.Write("ALTER TABLE ");
          writer.Write(this.Name(alterColumnOperation.Table));
          writer.Write(" ADD CONSTRAINT DF_");
          writer.Write(column.Name);
          writer.Write(" DEFAULT ");
          //...
    

    So EF doesn't try to make the constraint name unique.

    You should try the workaround and report it as a bug.

    EDIT: I've just realized that above mentioned Generate method is virtual so in the worst case you can inherit from SqlServerMigrationSqlGenerator and fix the SQL generation and set it as the sql generator in Configuration.cs:

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        SetSqlGenerator("System.Data.SqlClient", 
            new MyFixedSqlServerMigrationSqlGenerator());
    }
    

    EDIT 2:

    I think the best thing to do until it fixed to fall back to raw SQL:

    public override void Up()
    {
        Sql(@"ALTER TABLE [CustomerLocations] ADD CONSTRAINT 
            DF_CustomerLocations_DateLastUpdated 
            DEFAULT GETDATE() FOR [DateLastUpdated]");
        Sql(@"ALTER TABLE [CustomerLocations] ALTER COLUMN 
            [DateLastUpdated] [datetime]");
        //...
    }
    
    0 讨论(0)
  • 2020-12-06 14:35

    This solution tested in EF 6.1.3. most probably work on previous versions.

    You can implement a custom sql generator class derived from SqlServerMigrationSqlGenerator from System.Data.Entity.SqlServer namespace:

    using System.Data.Entity.Migrations.Model;
    using System.Data.Entity.SqlServer;
    
    namespace System.Data.Entity.Migrations.Sql{
        internal class FixedSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator {
            protected override void Generate(AlterColumnOperation alterColumnOperation){
                ColumnModel column = alterColumnOperation.Column;
                var sql = String.Format(@"DECLARE @ConstraintName varchar(1000);
                DECLARE @sql varchar(1000);
                SELECT @ConstraintName = name   FROM sys.default_constraints
                    WHERE parent_object_id = object_id('{0}')
                    AND col_name(parent_object_id, parent_column_id) = '{1}';
                IF(@ConstraintName is NOT Null)
                    BEGIN
                    set @sql='ALTER TABLE {0} DROP CONSTRAINT [' + @ConstraintName+ ']';
                exec(@sql);
                END", alterColumnOperation.Table, column.Name);
                    this.Statement(sql);
                base.Generate(alterColumnOperation);
                return;
            }
            protected override void Generate(DropColumnOperation dropColumnOperation){
                var sql = String.Format(@"DECLARE @SQL varchar(1000)
                    SET @SQL='ALTER TABLE {0} DROP CONSTRAINT [' + (SELECT name
                        FROM sys.default_constraints
                        WHERE parent_object_id = object_id('{0}')
                        AND col_name(parent_object_id, parent_column_id) = '{1}') + ']';
                PRINT @SQL;
                    EXEC(@SQL); ", dropColumnOperation.Table, dropColumnOperation.Name);
    
                        this.Statement(sql);
                base.Generate(dropColumnOperation);
            }
        }
    }
    

    and set this configuration:

    internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
    
            SetSqlGenerator("System.Data.SqlClient", new FixedSqlServerMigrationSqlGenerator ());
        }
        ...
    }
    
    0 讨论(0)
  • 2020-12-06 14:40

    Ok, based on nemesv's answer (accepted), here's how I ended up fixing the problem for now, until a fix is officially issued:

    internal class MyFixedSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator
    {
        protected override void Generate(AlterColumnOperation alterColumnOperation)
        {
            if (alterColumnOperation == null)
                throw new ApplicationException("alterColumnOperation != null");
    
            ColumnModel column = alterColumnOperation.Column;
            if ((column.DefaultValue != null) || !string.IsNullOrWhiteSpace(column.DefaultValueSql))
            {
                using (IndentedTextWriter writer = Writer())
                {
                    writer.Write("ALTER TABLE ");
                    writer.Write(this.Name(alterColumnOperation.Table));
                    writer.Write(" ADD CONSTRAINT DF_");
                    writer.Write(alterColumnOperation.Table + "_"); //   <== THIS IS THE LINE THAT FIXES THE PROBLEM
                    writer.Write(column.Name);
                    writer.Write(" DEFAULT ");
                    writer.Write(column.DefaultValue != null ? base.Generate(column.DefaultValue) : column.DefaultValueSql);
                    writer.Write(" FOR ");
                    writer.Write(this.Quote(column.Name));
                    this.Statement(writer);
                }
            }
            using (IndentedTextWriter writer2 = Writer())
            {
                writer2.Write("ALTER TABLE ");
                writer2.Write(this.Name(alterColumnOperation.Table));
                writer2.Write(" ALTER COLUMN ");
                writer2.Write(this.Quote(column.Name));
                writer2.Write(" ");
                writer2.Write(this.BuildColumnType(column));
                if (column.IsNullable.HasValue && !column.IsNullable.Value)
                {
                    writer2.Write(" NOT NULL");
                }
                this.Statement(writer2);
            }
        }
    }
    
    
    internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
    
            SetSqlGenerator("System.Data.SqlClient", new MyFixedSqlServerMigrationSqlGenerator());
        }
        ...
    }
    
    0 讨论(0)
提交回复
热议问题