【问题标题】:Dapper contrib get data having varchar primary keyDapper contrib 获取具有 varchar 主键的数据
【发布时间】: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


【解决方案1】:

尝试检查您的代码,例如:

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

【讨论】:

    猜你喜欢
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2017-12-19
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多