You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Index(nameof(Guid), IsUnique = true)]
public class TestEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public Guid Guid { get; set; }
public string Name { get; set; } = null!;
}
In DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasPostgresExtension("uuid-ossp");
modelBuilder.Entity<TestEntity>()
.Property<Guid>(x => x.Guid).HasDefaultValueSql("uuid_generate_v4");
}
and I try to call upsert with the following statement:
var result = await dbContext.UpsertRange<TestEntity>(
new TestEntity() { Guid = new Guid("780982ee-8379-420c-adf0-e99dbb5e58ed"), Name = "One" },
new TestEntity() { Guid = new Guid("704d01a1-8789-45b2-b90c-02e2274705c4"), Name = "Two" }
)
.On(x => x.Guid)
.AllowIdentityMatch()
.RunAsync();
Here is the sql its generated :
[Parameters=[@p0='?' (DbType = Int32), @p1='?' (DbType = Guid), @p2='?', @p3='?' (DbType = Int32), @p4='?' (DbType = Guid), @p5='?'], CommandType='Text', CommandTimeout='30']
INSERT INTO "TestEntities" AS "T" ("Id", "Guid", "Name") VALUES (@p0, @p1, @p2), (@p3, @p4, @p5) ON CONFLICT ("Guid") DO UPDATE SET "Id" = EXCLUDED."Id", "Name" = EXCLUDED."Name"
It throws an error to say that : Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "PK_TestEntities"
Why is the Id property in the sql insert statement? Id property is an auto generated column...
Entity:
and I try to call upsert with the following statement:
Here is the sql its generated :
It throws an error to say that :
Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "PK_TestEntities"
Why is the
Id
property in thesql
insert statement?Id
property is an auto generated column...FlexLabsUpsertTestExample.zip
The text was updated successfully, but these errors were encountered: