You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EF Core和ABP框架生成Guid主键实体时出错

问题:EF Core + ABP框架操作PostgreSQL数据库时主键类型修改报错

错误详情

控制台执行数据库命令时报错:

[11:58:34 ERR] Failed executing DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE "AppClienti" ALTER COLUMN "Id" TYPE uuid;
ALTER TABLE "AppClienti" ALTER COLUMN "Id"
DROP IDENTITY;

Visual Studio调试时抛出异常:

Npgsql.PostgresException: '22023: identity column type must be smallint, integer, or bigint'

实体类定义

public partial class Cliente : AggregateRoot<Guid>
{
    [StringLength(255)]
    public string RagioneSociale { get; set; }
    public double? OrdineLimite { get; set; }
    public float? Sconto1 { get; set; }
}

DbContext配置

builder.Entity<Cliente>(b =>
{
    b.ToTable(IlDiamanteConsts.DbTablePrefix + "Clienti", IlDiamanteConsts.DbSchema);
    b.ConfigureByConvention(); //auto-configure for the base class props
   ...properties mapping...
});

疑问

未对Id列做特殊映射,预期ConfigureByConvention()会自动处理主键配置。想知道如何解决该错误,以及在哪里配置主键使用newid生成值。


问题解决(更新1)

问题根源是初始迁移脚本中将主键定义为int类型,后续修改实体主键为Guid后,迁移脚本尝试修改列类型并移除IDENTITY约束时触发PostgreSQL的类型限制错误。重新创建迁移脚本后问题已解决。

内容的提问来源于stack exchange,提问作者advapi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 02:05:30