问题描述
HasKey()
方法在应用于 int
属性时创建 autoincrement PK。我需要一个 int
PK,但它没有自动增量。
解决方法
您可以通过在 HasKey
方法之后添加以下内容来扩展您的配置。
modelBuilder.Entity<Order>() .Property(c => c.Id) .ValueGeneratedNever();
如果您不打算使用“Fluent”(对于那些不想使用的人)。当您设置模型时,您可以将字段/属性装饰为主键。只是不包括
DatabaseGeneratedOption.Identity
[Key]
public int Id { get; set; }
这应该将表中的字段设置为主键,而不是 autoinc。