enkodellc / blazorboilerplate

Blazor Boilerplate / Starter Template with MudBlazor
MIT License
1.87k stars 371 forks source link

PostgreSQL: Error 42P10 - there is no unique or exclusion constraint matching the ON CONFLIC #1023

Open AnikeRU opened 1 year ago

AnikeRU commented 1 year ago

Hello

To avoid this error in Postgres add unique index to the table PluralTranslation in LocalizationDbContext.cs:

builder.Entity().HasIndex(e => new { e.LocalizationRecordId, e.Index }, "IX_LOCRECID_INDEX").IsUnique();

Else this row will be generate error 42P10 :

localization DbContext.Plural Translations.UpsertRange(plural Translations).On(l => new { l.Localization RecordId, l.Index }).RunAsync();

enkodellc commented 1 year ago

Thanks, would you care to share it as a PR?

AnikeRU commented 1 year ago

Yes, you can use this code in a blazorboilerplate project. New content of the LocalizationDbContext.cs:

` using BlazorBoilerplate.Infrastructure.Storage.DataModels; using Microsoft.EntityFrameworkCore;

namespace BlazorBoilerplate.Storage { public class LocalizationDbContext : DbContext { public LocalizationDbContext(DbContextOptions options) : base(options) { }

    public DbSet<PluralFormRule> PluralFormRules { get; set; }

    public DbSet<PluralTranslation> PluralTranslations { get; set; }

    public DbSet<LocalizationRecord> LocalizationRecords { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<LocalizationRecord>().HasIndex(c => new { c.MsgId, c.Culture, c.ContextId }).IsUnique();
        builder.Entity<PluralTranslation>().HasIndex(e => new { e.LocalizationRecordId, e.Index }, "IX_LOCRECID_INDEX").IsUnique();
    }
}

}`