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

Upsert on conflict - Alias instead of exclude #143

Open
Inexad opened this issue Jan 18, 2023 · 3 comments
Open

Upsert on conflict - Alias instead of exclude #143

Inexad opened this issue Jan 18, 2023 · 3 comments

Comments

@Inexad
Copy link

Inexad commented Jan 18, 2023

I currently have a problem with Upsert not updating my entries in database.

 await _context.Articles.UpsertRange(articles)
                .On(x => x.Id)
                .WhenMatched(x => new Article()
                {
                    AccessId = x.AccessId
                })
                .AllowIdentityMatch()
                .RunAsync(cancellationToken);

Above generates following:

INSERT INTO "Articles" AS "T" ("Id", "AccessId") 
VALUES ('12345', '0') 
ON CONFLICT ("Id") DO UPDATE SET "AccessId" =  "T"."AccessId"

That query will run without any errors but the AccessId in database won't be updated.

If i instead manually change the SQL to:

INSERT INTO "Articles" AS "T" ("Id", "AccessId") 
VALUES ('12345', '0') 
ON CONFLICT ("Id") DO UPDATE SET "AccessId" =  excluded."AccessId"

It works as intended.

Anyone have any idea what could be the problem here? I run .net 7, latest upsert package, postgresql 14.

@Inexad
Copy link
Author

Inexad commented Jan 18, 2023

When i remove the .WhenMatched() it seems to work? Then it produces the following SQL:

INSERT INTO "Articles" AS "T" ("Id", "AccessId") 
VALUES ('12345', '0') 
ON CONFLICT ("Id") DO UPDATE SET "AccessId" =  excluded."AccessId"

Any ideas why this is?

@Inexad
Copy link
Author

Inexad commented Feb 14, 2023

Anyone?

@xamele0n
Copy link

xamele0n commented Mar 9, 2023

It seems that you should use another version of method. see..

        /// <summary>
        /// Specifies which columns should be updated when a matched entity is found.
        /// The second type parameter points to the entity that was originally passed to be inserted
        /// </summary>
        /// <param name="updater">The expression that returns a new instance of TEntity, with the columns that have to be updated being initialised with new values</param>
        /// <returns>The current instance of the UpsertCommandBuilder</returns>
        public UpsertCommandBuilder<TEntity> WhenMatched(Expression<Func<TEntity, TEntity, TEntity>> updater)
        {

Try this

await _context.Articles.UpsertRange(articles)
                .On(x => x.Id)
                .WhenMatched((_, x) => new Article()
                {
                    AccessId = x.AccessId
                })
                .AllowIdentityMatch()
                .RunAsync(cancellationToken);

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