2

If the BO is marked as having a key with [key], why does dapper through an error?

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

    public string ReadyToWorkGuid { get; set; }

    public int QualificationID { get; set; }

}

Call to dapper that throws the exception

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

The table tblWebReadyToWorkQualifications has a primary key on TeacherID.

Error: at Dapper.Contrib.Extensions.SqlMapperExtensions.Get[T](IDbConnection connection, Object id, IDbTransaction transaction, Nullable`1 commandTimeout) in d:\egna projekt\dapper-dot-net\Dapper.Contrib\SqlMapperExtensions.cs:line 128\r\n

Juan
  • 4,910
  • 3
  • 37
  • 46
skunk
  • 83
  • 1
  • 8
  • 1
    What is the error being thrown? stacktrace? – Tewr Jun 06 '17 at 15:35
  • 1
    It should be noted that `Get()` is intended to get a single record, not a collection. `IEnumerable` isn't considered valid. You will likely have to write your own query for this. – willaien Jun 06 '17 at 16:10

2 Answers2

2

The error message is probably trying to tell you that the generic type IEnumerable<TeacherQualificationBO> does not have a [Key] attribute, which is true. Only the type TeacherQualificationBO does.

change

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

to

return connection.Get<TeacherQualificationBO>(TeacherID);

You also need to change the return type of your function to TeacherQualificationBO to reflect this. You state that TeacherID is a primary key (thus unique), so TeacherID will only ever correspond to a single record. Thus it makes sense not to return a collection.

Tewr
  • 3,713
  • 1
  • 29
  • 43
0

Might help someone. In my case I was using the wrong namespace. Instead of Dapper.Contrib.Extensions Visual Studio had added System.ComponentModel.DataAnnotations which also has a Key attribute.