I have a table with a default id
of newsequentialid
, but the values being generated are not sequential.
[Id] UNIQUEIDENTIFIER DEFAULT (newsequentialid()) NOT NULL
5f3ed690-5110-46c9-2ea8-08d68135eb22
73709d10-81b8-4fcd-2ea9-08d68135eb22
c2320a90-8cd9-4fc7-2eaa-08d68135eb22
a8b2a5a1-0e61-4562-2eab-08d68135eb22
fed58540-42d6-4644-2eac-08d68135eb22
de292204-c00c-49d1-2ead-08d68135eb22
59fe8541-9829-4fa1-2eae-08d68135eb22
90007035-5247-44a9-2eaf-08d68135eb22
dcdbc8dd-a409-435f-2eb0-08d68135eb22
3feba54b-9236-4dfe-2eb5-08d68135eb22
I'm populating the rows like this:
if (!dbContext.MyTable.Any())
{
for (int i = 0; i < 100; i++)
{
dbContext.MyTable.Add(new MyModel { Col1= "test", Col2= "test", Created = DateTime.UtcNow });
}
await dbContext.SaveChangesAsync();
}
Using SQL Server Express 2017 14.0.2002
I figured out I needed to add the following to the OnModelCreating
method in the ApplicationDbContext
modelBuilder.Entity<MyModel>().Property(x=>x.Id).HasDefaultValueSql("NEWSEQUENTIALID()");