我们有自己的命名对象的外部约定,我需要更改自动生成的外键约束的命名约定。现在看起来像: 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); }更多推荐
更改外键约束命名约定
发布评论