简体   繁体   English

更改外键约束命名约定

[英]Change foreign key constraint naming convention

We have our own external convention of naming objects and I need to change the naming convention for the auto generated foreign key constraints. 我们有自己的命名对象的外部约定,我需要更改自动生成的外键约束的命名约定。 Now it looks like: FK_dbo.City_dbo.CityType_City_CityTypeId but I would like it to be called City_FKC_CityType . 现在它看起来像: FK_dbo.City_dbo.CityType_City_CityTypeId但我希望它被称为City_FKC_CityType

I found a similar question which says that you can change the name of constraints manually. 我发现了一个类似的问题 ,说你可以手动更改约束的名称。 However, this does not suit me, since I have a lot of tables and foreign key constraints. 但是,这不适合我,因为我有很多表和外键约束。

I found some information about "Custom Code First Conventions" and I am wondering if I can change the name of constraint using this or if there are any methods to implement it? 我找到了一些关于“自定义代码优先约定”的信息,我想知道我是否可以使用此更改约束的名称,或者是否有任何方法来实现它?

Another variant is download the source code of EF, make changes and use that but that is in case of emergency. 另一个变体是下载EF的源代码,进行更改并使用它,但是在紧急情况下。

As a side note, I would also like to change the naming convention of the primary key. 作为旁注,我还想更改主键的命名约定。

You can implement a custom sql generator class derived from SqlServerMigrationSqlGenerator from System.Data.Entity.SqlServer namespace: 您可以从System.Data.Entity.SqlServer命名空间实现从SqlServerMigrationSqlGenerator派生的自定义sql生成器类:

public class CustomSqlGenerator : SqlServerMigrationSqlGenerator
{
    protected override void Generate(AddForeignKeyOperation addForeignKeyOperation)
    {
        addForeignKeyOperation.Name = getFkName(addForeignKeyOperation.PrincipalTable,
            addForeignKeyOperation.DependentTable, addForeignKeyOperation.DependentColumns.ToArray());
        base.Generate(addForeignKeyOperation);
    }

    protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation)
    {
        dropForeignKeyOperation.Name = getFkName(dropForeignKeyOperation.PrincipalTable,
            dropForeignKeyOperation.DependentTable, dropForeignKeyOperation.DependentColumns.ToArray());
        base.Generate(dropForeignKeyOperation);
    }

    private static string getFkName(string primaryKeyTable, string foreignKeyTable, params string[] foreignTableFields)
    {
        // Return any format you need
    }
}

Then you need to register your generator in DbContext using DbConfiguration : 然后,您需要使用DbConfigurationDbContext注册您的生成器:

public class CustomDbConfiguration : DbConfiguration
{
    public CustomDbConfiguration()
    {
        SetMigrationSqlGenerator(SqlProviderServices.ProviderInvariantName,
            () => new CustomSqlGenerator());
    }
}

And DbConfigurationTypeAttribute : DbConfigurationTypeAttribute

[DbConfigurationType(typeof(CustomDbConfiguration))]
public class YourEntities : DbContext

UPDATE: If you want to change a primary key name, you need to override following Generate methods: 更新:如果要更改主键名称,则需要覆盖以下Generate方法:

protected override void Generate(CreateTableOperation createTableOperation) 
{
    createTableOperation.PrimaryKey.Name = getPkName(createTableOperation.Name);
    base.Generate(createTableOperation);
}

protected override void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation)
{
    addPrimaryKeyOperation.Name = getPkName(addPrimaryKeyOperation.Table);
    base.Generate(addPrimaryKeyOperation);
}

protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation)
{
    dropPrimaryKeyOperation.Name = getPkName(dropPrimaryKeyOperation.Table);
    base.Generate(dropPrimaryKeyOperation);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM