C# Dapper contrib获取具有varchar主键的数据

C# Dapper contrib获取具有varchar主键的数据,c#,asp.net-core-mvc,dapper,dapper-contrib,C#,Asp.net Core Mvc,Dapper,Dapper Contrib,我有一个varchar键列。我正在尝试使用Dapper contrib的Get方法。我得到一个例外: Get仅支持具有[Key]或[ExplicitKey]属性的实体 我的实体: public class State : BaseModel { [Key] public string state_code { get; set; } public string state_name { get; set; } public int language_

我有一个varchar键列。我正在尝试使用Dapper contrib的Get方法。我得到一个例外:

Get仅支持具有[Key]或[ExplicitKey]属性的实体

我的实体:

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; }
}
我简洁的方法

public IEnumerable<State> FindByCode(string code)
{
    return this._db.Get<State>(code);
}
我甚至试着设置显式键,但还是得到了同样的错误。
我做错了什么?

尝试检查您的代码,如:

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; }
}

您使用的是System.ComponentModel.DataAnnotations.KeyAttribute还是Dapper.Contrib.Extensions.KeyAttribute?Dapper.Contrib.Extensions.KeyAttribute BaseModel中有什么?您确定已尝试过Dapper.Contrib.Extensions中的[ExplicitKey]吗?目前BaseModel中没有任何内容。它是为一般功能而创建的。它是建筑的一部分。是的,我试过使用Dapper.Contrib.Extensions中的[ExplicitKey],但它给了我相同的异常。@ros2008为什么返回IEnumerable,因为Get只返回一个对象?顺便说一句,对我来说,它同时使用[Key]和[ExplicitKey]