New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dapper.Contrib not passing key column with value #351
Comments
I just ran into the same issue.
Yes. Duplicate of #11. #11 was closed on June 7th with a claim that it works, but it would not then either. I would say Dapper.Contrib does not currently support inserts for application supplied keys, conflicting with the Dapper.Contrib readme.md:
My question is would the Dapper project welcome a patch to allow an application supplied key for inserts? I presume a new data annotation such as [NonAutoIncrementKey] or [ExplicitKey] would be an appropriate solution? |
True, the Insert() requires the key property to be autoincremented/generated by the database. The readme.md could be more specific in that regard for sure. I have code running on my machine with added support for an [ExplicitKey] attribute for Insert, Update, Get and Delete which also supports non-integers (guids, strings etc) as (explicit) key. Seems to run well, and no breaking changes of the current interface. Note that the Insert() extension normally always returns an integer with the new id, but when explicitkey is used, 0 (zero) is always returned from Insert(). Comments or thoughts around this? Personally I think it's very useful to have application supplied keys and I have use for it right now actually :) |
@johandanforth Sounds perfect! Thank you. |
@johandanforth While I think is another issue, |
@erick2red cheers for pointing at problem in contrib! If you think SCOPE_IDENTITY is an issue i MSSQL, could you please create a new issue for this? I think I know what you're thinking about :) |
Sent in a PR for this now, for some reason I've lost my write access to the repo :) I've also added few more tests for SQLite and MsSql and etc... Perhaps you want to have a peek @AndrewSmart @erick2red |
Fixes merged! |
I have same problem and finally found a solution: |
I'm still seeing this issue - I don't quite understand the comment from @bhx98. Was there a fix for explicitly stating the key? |
There is still an issue apparently. Even with [ExplicitKey] attribute this error is thrown sometimes. I am unable to find any pattern, it is completely random. |
Let me explain, because I could not find a proper title for this
I have this schema:
ObjectX(ObjectXId(Guid), Name(Varchar))
, and this POCONow, using Dapper.Contrib I write
And this code is failing with an exception inside Dapper, saying
ObjectXId
cannot be null. For some reason Dapper.Contrib is throwing away the value I passed as key.Is it assuming the
[Key]
column will be autogenerated, or autoincremented?The text was updated successfully, but these errors were encountered: