ID specification set to false

I use EF5 and Code First to create a database. When Entity has an Id field , EF create a Primary Key value in the database and set the Identity specification to true (automatically generated value). How to set Identity specification to false by default ?

+7
source share
1 answer

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); } } 
+19
source

All Articles