更改外键约束命名约定

编程入门 行业动态 更新时间:2024-10-25 19:30:51
本文介绍了更改外键约束命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

更改外键约束命名约定

本文发布于:2023-11-12 00:59:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580053.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!