Dapper.Contrib Insert Fails identity column

Created on 1 Jun 2016  路  5Comments  路  Source: StackExchange/Dapper

Cannot insert the value NULL into column 'Id', table 'Infusion.dbo.StockReference'; column does not allow nulls. INSERT fails.The statement has been terminated.

check the below images

2
3
1
4
5

The id column is an identity column but i keep getting the above error, i tried both ways assigning a value for Id and not assigning a value for the Id but still it doesn't work any thoughts? or am i missing something.

Most helpful comment

For posterity, the issue (for me anyway) was that I had specified [Key], instead of [ExplicitKey]. I had a one-to-one relationship table.

Thanks to joncloud for pointing me in the right direction.

All 5 comments

@mgravell

@ishamfazal how is StockReference defined exactly in the database? Does StockReference.Id have an Identity Specification?

@joncloud Thanks for your reply. i fixed the error

For posterity, the issue (for me anyway) was that I had specified [Key], instead of [ExplicitKey]. I had a one-to-one relationship table.

Thanks to joncloud for pointing me in the right direction.

Also for posterity, I had the same message when I had decorated my poco class id property with System.ComponentModel.DataAnnotations.KeyAttribute instead of Dapper.Contrib.Extensions.KeyAttribute. if you don't fully qualify the attribute this is very easy to miss.

Was this page helpful?
0 / 5 - 0 ratings