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
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
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.
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. :(.
Possibly the cause: #703 / (fixed in PR #704)
Any idea when this PR will be approved & merged ?
We're also experiencing this issue with the latest version.
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.
In case anyone else finds this useful, we added this workaround for #703 (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();
}
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
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?
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...
Thanks @HunterMcEwan--your workaround code worked for my case.
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
In case anyone else finds this useful, we added this workaround for #703 (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
Most helpful comment
In case anyone else finds this useful, we added this workaround for #703 (call this before any calls to
InsertAsync
)