How to configure an Identity column using Entity Framework Core?

Andrew Duffy picture Andrew Duffy · Apr 16, 2015 · Viewed 33.2k times · Source

How do I create an Auto increment identity column in Entity Framework Core?

Obviously I can do it using fluent API for EF6 for example.

Answer

vityanya picture vityanya · Mar 7, 2016

In latest version of EF7 there is a new extension method to set identity column

protected override void OnModelCreating(ModelBuilder modelBuilder)
{    
  modelBuilder.Entity<MyEntity>(b =>
  {
    b.HasKey(e => e.Identifier);
    b.Property(e => e.Identifier).ValueGeneratedOnAdd();
  });
}