看起来很简单,但我无法找到方法告诉 EF Core 不要创建带有自增标识列的实体的主键。 我想手动插入自己的主键值。我知道我可以使用属性来实现这一点,但我想知道如何通过 Fluent API 设置行为。我看到了 UseSqlServerIdentityColumn()
方法从 Property()
方法中,但我需要将其关闭(而不是打开)。我也尝试了以下代码,但它不起作用。
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Employees ON");
context.SaveChanges();
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Employees OFF");