DapperLib / DapperLib/Dapper.Contrib

Dapper.Contrib [ExplicitKey] attribute behaves like the [Key] attribute when using with IDbConnection.InsertAsync()

Open
#53 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
293
Forks
109
PR merge metrics
No merged PRs in 30d

Description

I have a model

public class TestModel
{
    [ExplicitKey]
    public int ExplicitId1 { get; set; }

    [ExplicitKey]
    public int ExplicitId2 { get; set; }

    public int Property1 { get; set; }
}

When I performed a IDbConnection.InsertAsync(), everything is working as expected

var model = new TestModel
{
    ExplicitId1 = 1,
    ExplicitId2 = 2,
    Property1 = 3
};

await dbConnection.InsertAsync(model);
// The SQL Statement that is being generated:
// INSERT INTO Test (ExplicitId1, ExplicitId2, Property1) VALUES (@ExplicitId1, @ExplicitId2, @Property1)

Now, things start to go wrong when I performed an IDbConnection.UpdateAsync() first followed by an IDbConnection.InsertAsync().

if (!await dbConnection.UpdateAsync(model))
{
    await dbConnection.InsertAsync(model);
    // The SQL Statement that is being generated:
    // INSERT INTO Test (Property1) VALUES (@Property1)
}

The INSERT SQL Statement that is being generated has excluded the Properties decorated with the ExplicitKey attribute. The columns ExplicitId1 and ExplicitId2 are composite key in the database table which aren't auto generated but have to be supplied. An error will be thrown if the above INSERT statement is executed since the columns aren't provided.

I think I have located the source of the problem in the source file SqlMapperExtensions.Async.cs
The line keyProperties.AddRange(explicitKeyProperties); adds "Explicit Key Properties" into the list of "Key Properties" effectively treating ExplicitKeys as Keys and caching them for subsequent uses. Property decorated with the Key attribute is treated as an auto-generated column in the database table and hence excluded from the INSERT statement generated.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Dapper.Contrib/SqlMapperExtensions.Async.cs around the referenced line where explicit key properties are added to key properties. Reproduce an UpdateAsync followed by InsertAsync using the two [ExplicitKey] properties, then verify that the subsequent INSERT includes both explicit key columns and their values.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.