1

Using MVC and Entity Framework 6 with Code First Migrations, EF out of box generates the PK Constraint names as dbo_TableName. I want to be able to rename those to "pkTableColColumn" I am aware that EF Core supports this.HasConstraintName("MyFKConstraint");

Unfortunately, I don't think EF 6 supports this through fluentAPI or data annotations.

I referenced this amazing post as a starting point. The major difference is that they wanted their PK constraint names to be "PKTable", where I would like mine to be named "pk[Table]Col[ColumnName]".

With a little changing of the PKNameGenerator class, mine ended up as follows:

public class PKNameGenerator : SqlServerMigrationSqlGenerator
{
    static readonly string PREFIX = "pk"; // prefix with pk

    // this one works as the Columns property exists in this context
    protected override void Generate(CreateTableOperation createTableOperation)
    {
        createTableOperation.PrimaryKey.Name = GetPkName(createTableOperation.Name, createTableOperation.PrimaryKey.Columns);
        base.Generate(createTableOperation);
    }

    // this one works as the Columns property exists in this context
    protected override void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation)
    {
        addPrimaryKeyOperation.Name = GetPkName(addPrimaryKeyOperation.Table, addPrimaryKeyOperation.Columns);
        base.Generate(addPrimaryKeyOperation);
    }

    // this one does not work as the Columns property does not exist in this context
    protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation)
    {
        dropPrimaryKeyOperation.Name = GetPkName(dropPrimaryKeyOperation.Table, dropPrimaryKeyOperation.Columns);
        base.Generate(dropPrimaryKeyOperation);
    }

    // Prefix + TableName + "Col" + ColumnName + And + ColumnName (and only applies with composite PKs)
    string GetPkName(string tableName, IList<string> columns)
    {
        string columnNaming = "";
        if (columns.Count > 1)
        {
            columnNaming = string.Join("And", columns.Select(c => c).ToArray());
        }
        else
        {
            columnNaming = columns[0];
        }

        return PREFIX + tableName.Substring(tableName.IndexOf('.') + 1) + "Col" + columnNaming;

    }
}

This worked perfectly when adding a new table with a PK or even a composite PK (which will result in pkTableColColumnAndColumn. The only issue i was having was when I would change a primary key and it would call protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation) Both the other override functions for createTableOperation and addPrimaryKeyOperation have the appropriate Columns objects populated. However, the dropPrimaryKeyOperation does not have the columns object.

I am a bit new to this API and would appreciate any ideas on how to get the dropPrimaryKey function to give me the current columns that are the PK.

Once I get this all sorted out, I am planning on using a similar process for renaming the FK Constraints.

Eric
  • 505
  • 5
  • 22

0 Answers0