Why Dapper Contrib only support an entity with a single key?

c# dapper dapper-contrib

Question

I useDapper and Dapper.Contrib when I deploy the production site in an MVC5 c# environment, I sometimes (!) receive issues saying:

GetAll<T> only supports an entity with a single [Key] or [ExplicitKey] property at Dapper.Contrib.Extensions.SqlMapperExtensions.GetSingleKey[T](String method) at Dapper.Contrib.Extensions.SqlMapperExtensions.GetAllAsync[T](IDbConnection connection, IDbTransaction transaction, Nullable`1 commandTimeout)

However, this only occurs roughly once every third deployment.

I have a suspicion that somehowDapper.Contrib automatically recognizes my main key since it has the name "Id", but I've adorned it with[ExplicitKey] (it's a GUID), and maybe those characteristics conflict. It may be something completely else, however.

Any suggestions for a solution to this issue other potentially renaming my main key?

A fragment of the shown model

[Table("Tasks")]
public class TasksModel
{
    [ExplicitKey]
    public Guid Id { get; set; }

...
1
9
8/25/2017 11:23:47 AM

Popular Answer

This, in my opinion, has something to do with the property's "id" name. When there are no properties with the [Key] property, the KeyCache construction code in Dapper/SqlMapperExtensions.cs looks like this.

var idProp = allProperties.Find(p => string.Equals(p.Name, "id", StringComparison.CurrentCultureIgnoreCase));

By adding [ExplicitKey] to your key in your model, you get both an ExplicitKey and a Key in your situation (by default as this property is called Id). I would use normal Dapper unless you have the option to modify the DB column name.

1
3/26/2019 4:41:30 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow