Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Dapper.Contrib not passing key column with value

Open erick2red opened this issue 11 years ago • 10 comments

Let me explain, because I could not find a proper title for this

I have this schema: ObjectX(ObjectXId(Guid), Name(Varchar)), and this POCO

public class ObjectX
{
  [Key]
  public Guid ObjectXId { get; set; }

  public string Name { get; set; }
}

Now, using Dapper.Contrib I write

var o = new ObjectX();
o.ObjectXId = mycustomGuid; // and this is unique
o.Name = "Foo";
sqlConnection.Insert(o);

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?

erick2red avatar Oct 09 '15 16:10 erick2red

I just ran into the same issue.

Is it assuming the [Key] column will be autogenerated, or autoincremented?

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:

For these extensions to work, the entity in question MUST have a key-property, a property named "id" or decorated with a [Key] attribute.

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?

AndrewSmart avatar Oct 09 '15 21:10 AndrewSmart

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 avatar Oct 13 '15 08:10 johandanforth

@johandanforth Sounds perfect! Thank you.

AndrewSmart avatar Oct 13 '15 16:10 AndrewSmart

@johandanforth While I think is another issue, SCOPE_IDENTITY doesn't work with MSSQL. I've have a tweaked version on my desktop using OUTPUT clause. I can send you a pull request if you like

erick2red avatar Oct 13 '15 17:10 erick2red

@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 :)

johandanforth avatar Oct 13 '15 17:10 johandanforth

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

johandanforth avatar Oct 14 '15 05:10 johandanforth

Fixes merged!

NickCraver avatar Nov 27 '15 14:11 NickCraver

I have same problem and finally found a solution: [key] in dapper contrib define as "specify the property as a key that is automatically generated by the database" (in my case key was not auto_increment with default value and in mysql it creates error!) [Explicti Key]: specify the property as a key explicity which is not automatically generated by the database) => I found that for solving this problem we must to have auto id column without change any thing in databse with remove [key] from class and add [ExplicitKey] my problem solve completely hope it helps you guys

bhx98 avatar Jul 08 '18 06:07 bhx98

I'm still seeing this issue - I don't quite understand the comment from @bhx98. Was there a fix for explicitly stating the key?

brendan87 avatar Jan 25 '19 00:01 brendan87

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.

See related stackoverflow post

rajivverma avatar Jul 29 '19 10:07 rajivverma