实体框架主键约束 [英] Entity Framework primary key constraint

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

问题描述

我正在将实体框架 6.0与 Microsoft SQL Server .默认情况下,它为表生成的主键的名称类似于 PK_dbo.TableName ,其中 TableName 为复数形式,即使表的实际名称位于单数形式.

I am using Entity Framework 6.0 with Microsoft SQL Server. By default, the primary key it generates for a table is named like PK_dbo.TableName, where TableName is in a plural form, even though the actual name of the table is in a singular form.

例如,我有一个名为 dbo.Employee 的表.但是,用EF 6表示的主键是 PK_dbo.Employees .

For example, I have a table named dbo.Employee. Yet the primary key denoted by EF 6 is PK_dbo.Employees.

有什么方法可以将主键更改为 TableName 为单数形式的 PK_TableName 吗?

Is there any way to change the primary key to something like PK_TableName where the TableName is in a singular form?

更新:感谢您的答复.我并不是要重命名为主键的列名.我喜欢的是重命名生成的数据库中的关键约束,这是我们团队中数据库人员的要求.抱歉,我没有在第一篇文章中说清楚.我确实在这篇文章中看到了一些讨论:实体框架4.1:名称约束.但是,尚未找到简单的解决方案.

Update: Thanks for the replies. I didn't mean to rename to column name of the primary key. What I like is to rename the key constraints in the generated database, which is requested by a database person in our team. Sorry that I didn't make it clear in my first post. I did see some discussions in this post: Entity Framework 4.1: Name constraints. However no simple solutions had been identified yet.

推荐答案

您可以这样做:

public class PKNameGenerator : SqlServerMigrationSqlGenerator {
        static readonly string PREFIX = "PK";
        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);
        }

        // Prefix + Table name without schema
        string GetPkName(string tableName) {
            return PREFIX + tableName.Substring(tableName.IndexOf('.')+1);
        }
    }

然后您需要像这样注册它:

And then you need to register it like that:

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

确保将上述类放置为与从DbContext派生的类在同一程序集中,或使用 DbConfigurationTypeAttribute

Make sure to place the above class in the same assembly as a class derived from DbContext or use the DbConfigurationTypeAttribute

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

来源: https://stackoverflow.com/a/31553476/3111429

更多信息: https://entityframework.codeplex.com/wikipage?title = Code%20First%20Annotations 部分 SQL生成

使用EF Core,您可以这样做:

Using EF Core you can do it like this:

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .HasKey(b => b.BlogId)
            .HasName("PrimaryKey_BlogId");
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

来源: http://ef.readthedocs.io/en/latest/modeling/relational/primary-keys.html#fluent-api

这篇关于实体框架主键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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