Dapper.Contrib Key caching issue

Created on 13 Feb 2017  路  11Comments  路  Source: StackExchange/Dapper

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.

contrib

Most helpful comment

We're waiting too.

The nature of the problem makes tons of existing code buggy.
Every place where there's an UpdateAsync is waiting for adding a InsertAsync afterwards to raise this issue.

All 11 comments

Related: #530 and #533

We also run into this issue. Since I don't want to branch off, and there is no merged fix, I ended up having to use reflection to call the SqlMapperExtensions.KeyPropertiesCache(type) to "warmup" the cache and work around the issue.

Ugh :(

Any idea when this fix will be merged?

I am waiting also for this fix

Another vote for this fix from me. (Is there a better way of indicating support for a fix than commenting here?)

We're waiting too.

The nature of the problem makes tons of existing code buggy.
Every place where there's an UpdateAsync is waiting for adding a InsertAsync afterwards to raise this issue.

We're also running into this problem. Luckily we caught this in unit tests and we were able to work around it before running the code in production. But I'm amazed that this issue has been around for over a year without a fix. The issue seems clear and there even is a proposed solution. I've tested the solution on my local machine and it seems to fix the issue. Please fix this :)

i just spent hours on this. I was really loving dapper to this point.

I got this error too(

I am running into this error too, a few days before going to Production...

We are also facing this issue. It's really sad that even 2 years after it is reported and even a fix is proposed, the code is not incorporated.

What is the alternative to fix this apart for clearing the cache or avoid using Asynchronous operations?

Was this page helpful?
0 / 5 - 0 ratings