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

EF Core搭配PostgreSQL:创建复合唯一索引失败求助

EF Core + Npgsql 复合唯一索引不生成问题排查

我目前使用EF Core搭配Npgsql(PostgreSQL)驱动插件进行数据库scaffolding,但发现创建复合唯一索引的EF代码无法像在MSSQL中那样正常工作。

Account与AccountReference为一对多关系,我将AccountId和Reference设为复合唯一索引,但生成数据库脚本时完全看不到该复合索引的生成痕迹。怀疑是bug,但也可能是操作有误,求帮忙排查问题。

以下是我的代码:

public class Account
{
    public Account() => AccountReferences = new List<AccountReference>();

    public int Id { get; init; }

    public IEnumerable<AccountReference> AccountReferences;
}

public class AccountReference
{
    public int Id { get; init; }

    public Account Account { get; set; }

    public int AccountId { get; set; }

    public string Reference { get; set; }
}

internal sealed class AccountReferenceConfiguration : IEntityTypeConfiguration<AccountReference>
{
    public void Configure(EntityTypeBuilder<AccountReference> builder)
    {
        builder.HasIndex(x => new { x.AccountId, x.Reference })
            .IsUnique();
    }
}

依赖版本:

  • Microsoft.EntityFrameworkCore v7.0.4
  • Microsoft.EntityFrameworkCore.Tools v7.0.4
  • Npgsql.EntityFrameworkCore.PostgreSQL v7.0.3

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 16:37:04