Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core auto-generated string primary key

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

like image 435
Kok How Teh Avatar asked May 17 '19 14:05

Kok How Teh


2 Answers

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

like image 162
Prakash G. R. Avatar answered Nov 19 '22 12:11

Prakash G. R.


Auto-generated primary key by using Fluent API

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Node>().Property(x => x.ID).HasDefaultValueSql("NEWID()");
}
like image 22
SUNIL DHAPPADHULE Avatar answered Nov 19 '22 12:11

SUNIL DHAPPADHULE