Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with AllowIdentityMatch #163

Open
rala144 opened this issue Aug 26, 2024 · 1 comment
Open

Problem with AllowIdentityMatch #163

rala144 opened this issue Aug 26, 2024 · 1 comment

Comments

@rala144
Copy link

rala144 commented Aug 26, 2024

Entity:

[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...

FlexLabsUpsertTestExample.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@rala144 and others