使用 EFCore 在 MariaDB 中生成自增 int 列
Generate auto-increment int column in MariaDB with EFCore
问题:
我正在尝试使用具有从 0 开始的自动递增整数主键的 codefirst 在 .netcore 2.2 中使用 efcore 创建一个 table。我发现的所有解决方案都会导致添加迁移时出错或自动递增有效,但从 int.MinValue.
开始
以下所有解决方案均已单独尝试,未合并:
解决方案一:
什么都不做。让 efcore 做它的默认事情。
-> 列自动递增但从 int.MinValue 开始
方案二:
在主键上设置注释 "[DatabaseGenerated(DatabaseGeneratedOption.Identity)]"
。
-> 列自动递增但从 int.MinValue 开始
方案三:
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 的自动递增整数主键列?
你不能。
MariaDB (MySQL) 将 0 视为创建新 AUTO_INCREMENT
值的意思。
不要假定 AUTO_INCREMENT
没有提供的属性。它只是说每个新值都将与现有值不同。
可能 int.MinValue
是一个很大的负值 (-2^31)。
问题:
我正在尝试使用具有从 0 开始的自动递增整数主键的 codefirst 在 .netcore 2.2 中使用 efcore 创建一个 table。我发现的所有解决方案都会导致添加迁移时出错或自动递增有效,但从 int.MinValue.
开始以下所有解决方案均已单独尝试,未合并:
解决方案一:
什么都不做。让 efcore 做它的默认事情。
-> 列自动递增但从 int.MinValue 开始
方案二:
在主键上设置注释 "[DatabaseGenerated(DatabaseGeneratedOption.Identity)]"
。
-> 列自动递增但从 int.MinValue 开始
方案三:
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 的自动递增整数主键列?
你不能。
MariaDB (MySQL) 将 0 视为创建新 AUTO_INCREMENT
值的意思。
不要假定 AUTO_INCREMENT
没有提供的属性。它只是说每个新值都将与现有值不同。
可能 int.MinValue
是一个很大的负值 (-2^31)。