简体   繁体   English

实体框架代码第一个主键约束名称

[英]Entity Framework Code First Primary Key Constraint Names

有什么方法可以控制EF在我的代码第一个定义的表上创建的PK索引的名称?

Yes, you can name it anything you like and put [Key] attribute on it. 是的,您可以随意命名,并在其上放置[Key]属性。

So if you have a model Person as below, you won't need anything like [Key] , iicallyt will treat PersonId as PK automatically with the convention {classname}Id 因此,如果您具有如下所示的Person模型,则不需要[Key]类的东西,iicalt会自动按照约定{classname}IdPersonId视为PK

public class Person
{
   public int PersonId { get;set; }

   public int FirstName { get; set; }
}

If you want to have some different PK, then you can specify it with [Key] as below: 如果要使用其他PK,则可以使用[Key]进行指定,如下所示:

 public class Person
 {
   [Key]
   public int YourPK{ get;set; }

   public int FirstName { get; set; }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM