C# 如何在 EF-Code-First 中指定主键名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13607512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Specify Primary Key Name in EF-Code-First
提问by tourdownunder
I'm using Entity Framework Codefirst to create my Database. The default Primary key with the schema name dbo.pk_Jobs seems to upset access 2007 when I connect to it over ODBC. If I manually edit the name and remove the schema name and rename this Primary Key to pk_jobs, Access can now read the table.
我正在使用实体框架 Codefirst 创建我的数据库。当我通过 ODBC 连接到 2007 时,模式名称为 dbo.pk_Jobs 的默认主键似乎扰乱了访问 2007。如果我手动编辑名称并删除架构名称并将此主键重命名为 pk_jobs,Access 现在可以读取该表。
Can I specify the Primary Key name to not include the name of the schema using Fluent Api, Data Attributes or any other method.
我可以使用 Fluent Api、Data Attributes 或任何其他方法指定主键名称以不包含架构名称吗?
public class ReportsContext : DbContext
{
public DbSet<Job> Jobs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Job>().ToTable("Jobs");
modelBuilder.Entity<Job>().HasKey(j => j.uuid);
base.OnModelCreating(modelBuilder);
}
}
public class Job
{
public Guid uuid{ get; set; }
public int active{ get; set; }
}
采纳答案by cubski
If you want to specify the column name and override the property name, you can try the following:
如果要指定列名并覆盖属性名,可以尝试以下操作:
Using Annotations
使用注解
public class Job
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("CustomIdName")]
public Guid uuid { get; set; }
public int active { get; set; }
}
Using Code First
使用代码优先
protected override void OnModelCreating(DbModelBuilder mb)
{
base.OnModelCreating(mb);
mb.Entity<Job>()
.HasKey(i => i.uuid);
mb.Entity<Job>()
.Property(i => i.uuid)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("CustomIdName");
}
Inside Migration Configuration
内部迁移配置
public partial class ChangePrimaryKey : DbMigration
{
public override void Up()
{
Sql(@"exec sp_rename 'SchemaName.TableName.IndexName', 'New_IndexName', 'INDEX'");
}
public override void Down()
{
Sql(@"exec sp_rename 'SchemaName.TableName.New_IndexName', 'Old_IndexName', 'INDEX'");
}
}
回答by Chris Dunaway
You can use the Keyattribute to specify the parts of the primary key. So your Job class might be
您可以使用Key属性来指定主键的部分。所以你的工作类可能是
public class Job
{
[Key]
public Guid uuid{ get; set; }
public int active{ get; set; }
}
The data annotation attributes are defined in the System.ComponentModel.DataAnnotations namespace
回答by Jeff Siver
If I understand, you are asking how to change the name of the primary key column used by Entity Framework. The following addition to your HasKey statement should take care of this:
如果我理解,您是在询问如何更改实体框架使用的主键列的名称。以下对您的 HasKey 语句的添加应注意这一点:
modelBuilder.Entity<Job>().Property(j => j.uuid).HasColumnName("pk_Jobs")
回答by savehansson
(This is a complement to the answer/comments by @Jeff Sivers and @cubski.)
(这是对@Jeff Sivers 和@cubski 的回答/评论的补充。)
As far as I know you can't specify the PK name with Data Attribute. Sometimes I need to get rid of the dbo.part of the name and I then use a manually edited code first migration to change the name, like this:
据我所知,您无法使用数据属性指定 PK 名称。有时我需要去掉dbo.名称的一部分,然后使用手动编辑的代码先迁移来更改名称,如下所示:
public partial class ChangeNameOnPKInCustomers : DbMigration
{
private static string fromName = "PK_dbo.Customers"; // Name to change
private static string toName = fromName.Replace("dbo.", "");
public override void Up()
{
Sql($"exec sp_rename @objname=N'[dbo].[{fromName}]', @newname=N'{toName}'");
// Now the PK name is "PK_Customers".
}
public override void Down()
{
Sql($"exec sp_rename @objname=N'[dbo].[{toName}]', @newname=N'{fromName}'");
// Back to "PK_dbo.Customers".
}
}
回答by Rajsanthosh Sreenivasan
When you add an explicit migration using Code First, you get a .cs file with the name of the migration which is a partial class with a base class DbMigration.
当您使用 Code First 添加显式迁移时,您将获得一个带有迁移名称的 .cs 文件,该文件是具有基类 DbMigration 的分部类。
For your Primary Key Constraint, you have either DropPrimaryKey or AddPrimaryKey function depending on what you are trying to do. My problem was with DropPrimaryKey as my Db had different name for the Primary Key.
对于您的主键约束,您可以使用 DropPrimaryKey 或 AddPrimaryKey 函数,具体取决于您要执行的操作。我的问题是 DropPrimaryKey,因为我的数据库有不同的主键名称。
Both these functions have overloads to take the name of the PK so that you could explicitly specify the name of the PK. Worked fine for me for DropPrimaryKey with explicit PK name. The argument being object anonymousArguments which is by default null.
这两个函数都有重载来获取 PK 的名称,以便您可以明确指定 PK 的名称。对于具有显式 PK 名称的 DropPrimaryKey,对我来说效果很好。参数是对象anonymousArguments,默认情况下为null。

