EF Core Fluent API (How to remove identity from primary key)
EF Core Fluent API (How to remove identity from primary key)
Seems simple but I can't figure out how to tell EF core not to create the primary key of an entity with an auto-incrementing identity column. I want to insert my own primary key values myself. I realize I can do this using attributes, but I'd like to know how to set behavior via fluent API. I see the UseSqlServerIdentityColumn() method from the Property() method but I need to turn it off (not on). I've also tried the following code but it doesn't work.
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Employees ON");
context.SaveChanges();
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Employees OFF");
1 Answer
1
https://docs.microsoft.com/en-us/ef/core/modeling/generated-properties
modelBuilder.Entity<EntityType>()
.Property( et => et.Id )
.ValueGeneratedNever();
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy