-1

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?

Palle Due
  • 5,929
  • 4
  • 17
  • 32
ros2008
  • 21
  • 7

1 Answers1

3

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; }
}
Edward
  • 28,296
  • 11
  • 76
  • 121