.Net Core auto-generated string primary key

6.7k Views Asked by At

.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?

2

There are 2 best solutions below

1
Prakash G. R. On BEST ANSWER

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

0
SUNIL DHAPPADHULE On

Auto-generated primary key by using Fluent API

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Node>().Property(x => x.ID).HasDefaultValueSql("NEWID()");
}