DapperLib / DapperLib/Dapper.Contrib

Dapper.Contrib Key caching issue

Open
#21 11 comments 6 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

There is a bug in the caching logic of the KeyProperties. Method UpdateAsync() and DeleteAsync() alter the original cached list of properties key code ref.
So, if the first operation is a Update or Delete the "KeyPropertiesCache" return a pointer to the original list and the Update/Delete method add the explicitKeyProperties to the list. When the Insert() get called the "allPropertiesExceptKeyAndComputed" is wrong.

Test to reproduce it:

    public async Task TestMethod()
    {
        var myClass = new MyClass()
        {
            AccountId = Guid.NewGuid(),
            Name = "Some name"
        };

        using (IDbConnection db = new SqlConnection(connectionString))
        {
            //var x = await db.InsertAsync(myClass); // Add and test pass
            var t = await db.UpdateAsync(myClass);
            var r = await db.InsertAsync(myClass);                
        }
    }

Suggested fix that all returns from cache-layer (KeyPropertiesCache, ExplicitKeyPropertiesCache, ComputedPropertiesCache etc.) should call .ToList() on the item before return it.

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

The relevant cache logic is in Dapper.Contrib/SqlMapperExtensions.Async.cs around line 217. Start with UpdateAsync, DeleteAsync, InsertAsync, and the KeyPropertiesCache behavior described in the issue, then reproduce the provided UpdateAsync-then-InsertAsync sequence. Done means cached property lists are not altered by update or delete operations and the regression case passes.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.