Consider the following POCO class:
public class Album { public Guid ID { get; set; } public DateTime DateCreated { get; set; } }1) Data Annotations
Decorate your Primary Key with DatabaseGenerated / DatabaseGenerationOption.Identity.
[DatabaseGenerated(DatabaseGenerationOption.Identity)] public Guid ID { get; set; }Note: you can also use the "Key" attribute, but since the name of my property is ID Entity Framework will automatically detect that this field is a primary key.
2) Fluent API
In your DbContext class, override the OnModelCreating method and add the following line:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Album>().Property(p => p.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); }
Will it be "DatabaseGenerationOption" or "DatabaseGeneratedOption" ?
ReplyDelete