【问题标题】:EF core "cannot insert explicit value for identity column" when using BYTE as identity column type in Sql Server在 Sql Server 中使用 BYTE 作为标识列类型时,EF 核心“无法为标识列插入显式值”
【发布时间】:2021-07-15 03:06:18
【问题描述】:

我在程序启动期间使用 EF Core 将数据播种到 SQL 数据库时遇到问题。

我有一个这样定义的 EF Core 表:

migrationBuilder.CreateTable(
            name: "PoolStyles",
            columns: table => new
            {
                Id = table.Column<byte>(type: "tinyint", nullable: false)
                    .Annotation("SqlServer:Identity", "1,1"),
                Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256),
                Code = table.Column<string>(type: "nvarchar(40)", maxLength: 40),
                Description = table.Column<string>(type: "nvarchar(MAX)", nullable: true),
                IsRestricted = table.Column<bool>(type: "bit"),
                Priority = table.Column<byte>(type: "tinyint", nullable: true, defaultValue: 0),
                Icon = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_PoolStyles", x => x.Id);
            });

这里是匹配的实体类:

public class PoolStyle
{
    public byte Id { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public bool IsRestricted { get; set; }
    public byte Priority { get; set; }
    public string Icon { get; set; }
    public string Description { get; set; }
}

在我的应用程序启动期间,我尝试将一些数据播种到我的数据库中,如下所示:

        if (!context.PoolStyles.Any())
        {
            context.PoolStyles.Add(new PoolStyle { Code = "SRV", Description = "Survival Pool", Icon = "", IsRestricted = false, Name = "Survival" });
            await context.SaveChangesAsync();
        }

但是我不断收到以下错误:

迁移或植入数据库时​​发生错误。

Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时出错。有关详细信息,请参阅内部异常。

Microsoft.Data.SqlClient.SqlException (0x80131904):当 IDENTITY_INSERT 设置为 OFF 时,无法在表“PoolStyles”中插入标识列的显式值。

错误之前是 SQL EF Core 尝试运行:

失败:Microsoft.EntityFrameworkCore.Database.Command[20102] 执行 DbCommand (11ms) 失败 [Parameters=[@p0='?' (大小 = 1) (DbType = 字节), @p1='?' (大小 = 4000),@p2='?' (大小 = 4000), @p3='?' (大小 = 4000),@p4='?' (DbType = Boolean), @p5='?' (大小 = 4000), @p6='?' (Size = 1) (DbType = Byte)], CommandType='Text', 命令超时='30'] 设置无计数; 插入 [PoolStyles]([Id]、[Code]、[Description]、[Icon]、[IsRestricted]、[Name]、[Priority]) 值(@p0、@p1、@p2、@p3、@p4、@p5、@p6);

问题:您可以看到 EF Core 正在将 [Id][Priority] 列添加到执行的 SQL 中,但这些值不包含在我尝试传递给 @ 的对象中987654327@函数。

另外,我没有使用注释,因此无法使用它们来解决这个问题,我“手动编码”迁移,因为我需要的东西很复杂。

问题:谁能帮我弄清楚为什么 EF Core 试图将这些值添加到插入语句中?

【问题讨论】:

  • 这可能是由于数据类型和约定检查所致?尝试将 [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 添加到 Id 字段。
  • EF 假设 Id 是 convention 的标识列。最简单的解决方案是重命名类中的属性并使用 [Column] 标记将属性与表中的列相关联。
  • 感谢 nicholas 和 steve,这最终成为了一个数据类型问题,您的 cmets 让我朝着正确的方向前进。

标签: c# sql-server entity-framework entity-framework-core


【解决方案1】:

问题是使用 byte 作为标识列的数据类型。我不知道为什么我知道我可以在 sql server 中手动创建一个类型为 byte 的标识列。 EF Core 中的某些东西令人窒息。

解决方案:

我将标识列数据类型更改为 int 并且插入可以正常使用自动递增的 id 值。

也许我错过了其他东西,但它的工作和这些表不会有很多记录,所以我将使用 int 并继续,哈哈。

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 2017-12-31
    • 2021-05-15
    • 2015-10-27
    • 1970-01-01
    • 2020-02-03
    • 2019-02-21
    • 2016-04-13
    相关资源
    最近更新 更多