【发布时间】:2019-08-27 10:19:20
【问题描述】:
我有一个 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);
}
我什至尝试设置显式密钥,但我仍然得到同样的错误。 我做错了什么?
【问题讨论】:
-
你使用的是
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]
标签: c# asp.net-core-mvc dapper dapper-contrib