Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework auto incrementing field, that isn't the Id

I know this isn't the most ideal solution, but I need to add an auto incrementing field to one of my EF Code First objects. This column id NOT the Id, which is a guid.

Is there anyway for me to define the auto incrementing field in code, or would creating the column myself and defining in the DB that its auto incrementing work?

like image 374
JamesStuddart Avatar asked May 03 '12 08:05

JamesStuddart


1 Answers

You can annotate that property with DatabaseGenerated(DatabaseGeneratedOption.Identity). EF allows only single identity column per table.

public class Foo {     [Key]     public Guid Id { get; set; }      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]     public long Bar { get; set; } } 
like image 150
Eranga Avatar answered Oct 02 '22 22:10

Eranga