DapperLib / Dapper.Contrib

Dapper community contributions - additional extensions for Dapper
Other
258 stars 93 forks source link

ExplicitKey value is occasionally null upon insert #52

Open robertracz opened 7 years ago

robertracz commented 7 years ago

Hey Guys!

Just wondering if you've experienced this issue before. We've been using dapper (1.50.0 now) for a while and it all works great but in the past couple of weeks we started seeing null values for explicit primary keys upon inserting objects. It happens randomly, sometimes for one insert but sometimes for all the inserts.

We added logging and the key property always has the value right before the insert but the insert still throws the primary key cannot be null exception. Recycling the app pool fixes it all the time so it might not be dapper related but something with the environment... still wondering if you've ever encountered something like this with Dapper.

Thank you in advance!

Rob

nghialt2 commented 7 years ago

Hi Rob,

I also have encounterd that issue but still dont know the solution to resolve it. I hope that everyone know this issue and help us fix it. :(

Best regard, Nghia Luu

robertracz commented 7 years ago

Yea it's a weird one. I spent some time on it and my guess is that at some point in time the ConcurrentDictionary with the explicit keys gets compromised or cleared and every insert that happens afterwards start failing for that one object type. The occurrence is (seemingly) totally random, some combination of things trigger it and boom. In some cases it's even running on one server but is failing on the other (and of course it never ever happens locally:)).

For now we just added a plain old sql for that object and I haven't had time to dig into it more since.

nghialt2 commented 7 years ago

Yup. I also have the same issue. Run local is OK but deploy to server then sometimes happens that error. My solution uses temporarily the excute command in order to insert data. :(.

HunterMcEwan commented 7 years ago

Possibly the cause: DapperLib/Dapper.Contrib#21 / (fixed in PR DapperLib/Dapper#704)

IntoTheNature commented 6 years ago

Any idea when this PR will be approved & merged ?

jsign commented 6 years ago

We're also experiencing this issue with the latest version.

Mmmattias commented 6 years ago

We are experiencing this 100% of the time in our production environment.

The same code runs just fine locally but our production environment fails with null key exception 100% of the time.

We use version 1.50.0.

HunterMcEwan commented 6 years ago

In case anyone else finds this useful, we added this workaround for DapperLib/Dapper.Contrib#21 (call this before any calls to InsertAsync)

private static void DapperHack()
        {
            var cache = typeof(SqlMapperExtensions).GetField("KeyProperties", BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null)
                as ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>;

            cache?.Clear();
        }
robertracz commented 6 years ago

i believe it got fixed before but not for async:

that line holds a ref to the cache in case you're calling update for the first time

https://github.com/StackExchange/Dapper/blob/80231b4d2b2391bbb01c2cd7be75fbecfe0c0296/Dapper.Contrib/SqlMapperExtensions.cs#L395

Slettan commented 6 years ago

We are seeing this a lot in our test and production environments when using InsertAsync. The entity has an Id property which is a Guid marked with the ExplicitKey attribute. Are there any plans to look into this issue?

mrtristan commented 6 years ago

i'm also experiencing this. I'm able to consistently make it happen or not happen in two different tracks of code. glad i found this issue so i'm feeling a little less insane.

in one flow, I'm moving data between databases and triggering this bug every time.

in another flow, i'm inserting new objects that i instantiate fresh, and it does not occur.

i think this issue has caused me to age a few years...

JohnnyFun commented 5 years ago

Thanks @HunterMcEwan--your workaround code worked for my case.

szmcdull commented 4 years ago

Is this issue fixed? 'Cause I just encountered it. I have a nullable DateTime key column with DEFAULT CURRENT_TIMESTAMP(3). When inserting a new instance, I got MySql.Data.MySqlClient.MySqlException (0x80004005): Column 'addTime' cannot be null

farshid3003 commented 4 years ago

In case anyone else finds this useful, we added this workaround for DapperLib/Dapper.Contrib#21 (call this before any calls to InsertAsync)

private static void DapperHack()
        {
            var cache = typeof(SqlMapperExtensions).GetField("KeyProperties", BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null)
                as ConcurrentDictionary<RuntimeTypeHandle, IEnumerable<PropertyInfo>>;

            cache?.Clear();
        }

this clearing cache worked for me too