EF Core Fluent API (How to remove identity from primary key)

Viewed 9264

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");
2 Answers

With attribute it is also possible.

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
Related