Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper's [ExplicitKey] attribute - what's the purpose?

Tags:

dapper

What is the purpose of Dapper's [ExplicitKey] attribute? In this example generated SQL is absolutely identical, regardless of whether the key is applied or not.

exec sp_executesql 
N'insert into InvoiceDetail ([InvoiceID], [Detail]) values (@InvoiceID, @Detail);
select SCOPE_IDENTITY() id',N'@Detail nvarchar(4000),@InvoiceID int',@Detail=N'Insert_Single_1',@InvoiceID=4
like image 202
Sergei Avatar asked Mar 22 '18 12:03

Sergei


1 Answers

This is why the attribute exists:

https://github.com/StackExchange/Dapper/issues/351

The [Key] attribute assumes an autoincremented key, and when you try to pass a custom (e.g. non-int) key instead, Insert and InsertAsync fail with NullReferenceException, even if the passed value clearly isn't null. So [ExplicitKey] was introduced to handle those instead.

like image 158
Darth Veyda Avatar answered Oct 18 '22 07:10

Darth Veyda