【发布时间】:2019-03-30 17:01:05
【问题描述】:
我正在尝试使用 dapper 从 mysql 数据库中检索数据,但结果集 id(主键)和外键为空值。其他属性也有值。
我尝试将 sql 查询从 select * from course 更改为 select id,name,did from courses 的完整形式。
Course{
public Course()
{
}
public string Id { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public bool Is_Elective { get; set; }
public string DId { get; set; }
public int Sem { get; set; }
}
class CourseDAO
{
private readonly MySqlConnection conn;
private string connectionString = "Server=localhost;Database=university;Uid=root;Pwd=*****;";
public CourseDAO()
{
conn = new MySqlConnection(connectionString);
}
public List<Course> getAll()
{
string sql = "select * from university.course";
List<Course> courses = conn.Query<Course>(@sql).ToList();
return courses;
}
}
预期: 课程列表包含来自 db 的所有课程的正确值。
实际 课程列表包含来自 db 的所有课程,其 id 为 null,rest 有值。
【问题讨论】:
-
您的班级中的属性名称不正确。将 ID 更改为 course_id,将 DId 更改为 department_id,反之亦然。默认情况下,dapper 尝试按名称应用值(使用忽略大小写策略)。