6

.Net Core 2.2 Identity framework has string primary key which is an auto-generated GUID. My question is how to have an auto-generated STRING primary key using Entity Framework 2.2?

Kok How Teh
  • 3,298
  • 6
  • 47
  • 85

2 Answers2

7

You need to have the attribute [DatabaseGenerated(DatabaseGeneratedOption.Identity)] added to the column. See the official documentation for more details.

Prakash G. R.
  • 4,746
  • 1
  • 24
  • 35
4

Auto-generated primary key by using Fluent API

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Node>().Property(x => x.ID).HasDefaultValueSql("NEWID()");
}
SUNIL DHAPPADHULE
  • 2,755
  • 17
  • 32