【发布时间】:2019-11-20 19:46:17
【问题描述】:
问题:
我正在尝试使用 codefirst 在 .netcore 2.2 中创建一个带有 efcore 的表,该表具有一个从 0 开始的自动增量整数主键。我发现的所有解决方案都会导致添加迁移时出错或自动增量工作但从 int.MinValue 开始。
已单独尝试过以下所有解决方案,而不是组合:
解决方案 1:
什么都不做。让 efcore 做它默认的事情。
-> 列自动递增,但从 int.MinValue 开始
解决方案 2:
在主键上设置注释"[DatabaseGenerated(DatabaseGeneratedOption.Identity)]"。
-> 列自动递增,但从 int.MinValue 开始
解决方案 3:
modelBuilder.Entity<User>()
.Property(u => u.Id)
.ValueGeneratedOnAdd();
-> 列自动递增,但从 int.MinValue 开始
解决方案 4:
modelBuilder.HasSequence<int>("User_seq")
.StartsAt(0)
.IncrementsBy(1);
modelBuilder.Entity<User>()
.Property(u => u.Id)
.HasDefaultValueSql("NEXT VALUE FOR User_seq");
-> 添加迁移失败并出现错误:
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SEQUENCE User_seq START WITH 0 INCREMENT BY 1' at line 1"
解决方案 5:
将 Id 的数据类型更改为 uint。
-> 列自动递增,但从 int.MaxValue 开始
我的用户实体:
public class User
{
public int Id { get; set; }
public string FirstName{ get; set; }
public string LastName { get; set; }
}
使用的提供者: Pomelo.EntityFrameworkCore.MySql
澄清我的问题:如何使用 efcore codefirst 在 MariaDB 中创建从 0 开始递增 1 的自动递增整数主键列?
【问题讨论】:
标签: c# .net-core entity-framework-core mariadb auto-increment