Dapper异常 - Get <t>仅支持具有[Key]属性的en实体,但定义了[key]

时间:2017-06-06 15:30:23

标签: c# sql-server dapper dapper-contrib

如果BO被标记为带有[key]的键,为什么错误地显示错误?

[Table("tblWebReadyToWorkQualifications")]
public class TeacherQualificationBO
{
    [Key]
    public int TeacherID { get; set; }

    public string ReadyToWorkGuid { get; set; }

    public int QualificationID { get; set; }

}

调用抛出异常的dapper

      public IEnumerable<TeacherQualificationBO> GetTeacherQualifications(int TeacherID)
      {
        using (var connection = GetOpenConnection())
        {
            return connection.Get<IEnumerable<TeacherQualificationBO>>(TeacherID);
        }
      }

表tblWebReadyToWorkQualifications在TeacherID上有一个主键。

错误: at Dapper.Contrib.Extensions.SqlMapperExtensions.Get [T](IDbConnection连接,Object id,IDbTransaction事务,Nullable`1 commandTimeout)在d:\ egna projekt \ dapper-dot-net \ Dapper.Contrib \ SqlMapperExtensions.cs:line 128 \ r \ n

1 个答案:

答案 0 :(得分:1)

错误消息可能试图告诉您泛型类型public class App { public static void main(String[] args) { App app = new App(); try { app.print(10, 60); } catch (ArithmeticException ae) { // ignore } } private void print(int next, int until) { System.out.println(next); assertNotEndOfRange(next, until); print(++next, until); } private int assertNotEndOfRange(int next, int until) { return 0 / (until - next); } } 没有[Key]属性,这是真的。只有IEnumerable<TeacherQualificationBO>类型。

更改

TeacherQualificationBO

return connection.Get<IEnumerable<TeacherQualificationBO>>(TeacherID);

您还需要将函数的返回类型更改为return connection.Get<TeacherQualificationBO>(TeacherID); 以反映这一点。您声明TeacherQualificationBO是主键(因此是唯一的),因此TeacherID将仅对应于单个记录。因此,不返回集合是有道理的。