If you do not want to use identification keys, you have several options.
Option 1: You can globally disable this feature by deleting StoreGeneratedIdentityKeyConvention :
public class YourContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>(); } }
You can selectively select keys and change the behavior for them using an attribute or free mapping.
Option 2: Attribute:
public class MyEntity { [DatabaseGenerated(DatabaseGeneratedOption.None)] public int Id { get; set; } }
Option 3: Free API:
public class YourContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<MyEntity>() .Property(e => e.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); } }
Ladislav Mrnka
source share