Dapper contrib get data having varchar primary key

1.2k Views Asked by At

I have a varchar key column. I am trying to use the Get method of Dapper contrib. I get an exception:

Get only supports an entity with a [Key] or an [ExplicitKey] property.

My Entity:

public class State : BaseModel
{
    [Key]       
    public string state_code { get; set; }
    public string state_name { get; set; }
    public int language_code { get; set; }
    public bool is_active { get; set; }
}

My Dapper Method

public IEnumerable<State> FindByCode(string code)
{
    return this._db.Get<State>(code);
}

I even tried to set Explicit Key, still I get the same error. What am I doing wrong?

1

There are 1 best solutions below

0
On

Try to check your code like:

public class State : BaseModel
{
    [System.ComponentModel.DataAnnotations.Key]
    [Dapper.Contrib.Extensions.Key]
    public string state_code { get; set; }
    public string state_name { get; set; }
    public int language_code { get; set; }
    public bool is_active { get; set; }
}