Dapper.Contrib - Get and Insert methods fail occasionally(!) when [ExplicitKey] is used
We have a table with a SqlHierarchyId primary key, and on some occasions InsertAsync on it fails with "Cannot insert Null into Id field", as if the key isn't marked with [ExplicitKey] (it is): https://stackoverflow.com/questions/49669149/dapper-contrib-insertasync-fails-to-insert-an-hierarchyid-key
It starts happening randomly and then stops happening randomly with the exact same data. Most of the time InsertAsync work just fine, and I have no idea how to reproduce the issue.
Get and GetAsync exhibit similar behaviour for complex keys. We have this problem with SqlHierarchyId, and the topicstarter with Guid key, but the exception is the same ("GetAll<T> only supports an entity with a single [Key] or [ExplicitKey] property"), and again, it happens rarely and randomly, and is impossible to reproduce: https://stackoverflow.com/questions/45880051/dapper-contrib-only-supports-an-entity-with-a-single-key-or-explicitkey?rq=1
The "solution" in both cases is to wait until the problem is gone (for me it's usually gone after 10-15 attempts) or simply use QueryAsync/ExecuteAsync instead, but that doesn't look like much of a solution. Is there any chance this can be looked at?
Thank you in advance!
I've encountered this before, in some sort of way. I tend to get this happening when I try to insert, after getting the data from a post or something similar like that. Check to make sure it's actually random, and not happening in a specific spot, where the ID may be getting nulled. dotnet can be dumb sometimes, and IDs may get lost in translation, make sure you are verifying that the ID is not null, before you insert said item, then debug on that if statement, to see what may be causing this. My issue is not exactly the same as yours, however, as I've never used a SqlHierarchyId type, and I'm not sure dapper has support for that datatype, either.
Also, does this issue occur if you do the none-async version? async causes LOTS of issues, check and see if it still happens when you make them concurrent.