Reputation: 5027
I am using CodeFirst in my project. One of the things I am learning is how I can design my tables by first writing classes. My question is - how can I classify the primary key [key] Id, as an auto increment field. So that when a record is created (or a row in the table) the primary key is auto generated. How do I do this in codefirst while writing class definition. Thanks for the help.
public class NewTable
{
//Looking for something like [AutoIncrement][key] for Id field etc...
[key]
public int Id { get; set; }
[Required(ErrorMessage = "Title is required")]
[DisplayName("Title")]
public string Title { get; set; }
}
Upvotes: 0
Views: 2534
Reputation: 52725
A string
auto increment? That's not gonna work.
Use int
instead, and the PK will be an identity
column by default. You don't need the [Key]
attribute either; EF will infer that because the property is called Id
.
Upvotes: 1