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
相关产品推荐
相关产品推荐

