更改外键约束命名约定 [英] Change foreign key constraint naming convention

查看:141
本文介绍了更改外键约束命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有自己的命名对象的外部约定,我需要更改自动生成的外键约束的命名约定。现在看起来像: FK_dbo.City_dbo.CityType_City_CityTypeId 但我希望它被称为 City_FKC_CityType

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.

我发现一个类似的问题,表示您可以更改约束名称手动。但是,这并不适合我,因为我有很多表和外键限制。

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?

另一个变体是下载EF的源代码,进行更改并使用,但是在紧急情况下。

Another variant is download the source code of EF, make changes and use that but that is in case of emergency.

作为附注,我还要更改主键的命名约定。

As a side note, I would also like to change the naming convention of the primary key.

推荐答案

您可以从 System.Data.Entity.SqlServer SqlServerMigrationSqlGenerator 派生的自定义sql生成器类c $ c> namespace:

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

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

然后,您需要在 DbContext中注册您的生成器使用 DbConfiguration

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

DbConfigurationTypeAttribute

And 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:

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

这篇关于更改外键约束命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆