c#-Dapper查询结果在对象中将表的Pk设置为空
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-Dapper查询结果在对象中将表的Pk设置为空,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2621字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试使用dapper从mysql数据库检索数据,但结果将id(主键)和外键设置为null.其他属性具有值.
我试图将SQL查询从选择*从课程更改为完整形式,作为选择ID,名称,从课程的ID.
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为ID的所有课程,并且null和rest都具有值.
解决方法:
即使问题已在Maxim的问题注释中解决,我还是要用很少的解决方案来描述问题.
问题原因:
Dapper按名称执行从sql查询结果到对象的映射. SQL查询结果字段“标题”自动映射到Course.Title(映射不区分大小写).
在您的情况下,数据库列与C#属性之间存在两个名称不匹配的情况:(course_id!= Id和department_id!= DId),因此Dapper无法映射这些字段.
解决方案1,SQL列别名
您可以按照以下方式在sql查询中列出具有可能的列别名的表列:
string sql = "select course_id Ad Id, title, credits, Is_elective, department_id as DId, sem from university.course";
使用sql中的显式列名,Dapper可以执行基于名称的自动映射.
解决方案2,Dapper自定义映射
Dapper Custom mapping是用于为每个对象手动定义哪个列映射到哪个属性的功能.
这是处理映射的类(从another SO answer借用的双向映射的思想):
public class ColumnMap
{
private readonly Dictionary<string, string> mappings = new Dictionary<string, string>();
public void Add(string t1, string t2)
{
mappings.Add(t1, t2);
}
public string this[string index]
{
get
{
// Check for a custom column map.
if (forward.ContainsKey(index))
return forward[index];
if (reverse.ContainsKey(index))
return reverse[index];
// If no custom mapping exists, return the value passed in.
return index;
}
}
}
设置ColumnMap对象,并告诉Dapper使用该映射.
var columnMap = new ColumnMap();
columnMap.Add("Id", "course_id");
columnMap.Add("DId", "department_id");
SqlMapper.SetTypeMap(typeof (Course), new CustomPropertyTypeMap(typeof (Course), (type, columnName) => type.GetProperty(columnMap[columnName])));
解决方案3,动态类型和LINQ
您可以使用动态对象执行字段映射,如下所示:
string sql = "select * from university.course";
List<Course> courses = conn.Query<dynamic>(@sql)
.Select(item => new Course()
{
Id = item.course_id,
Title = item.title,
Credits = item.credits,
Is_Elective = item.Is_elective,
DId = department_id,
Sem = sem
})
.ToList();
内容总结
以上是互联网集市为您收集整理的c#-Dapper查询结果在对象中将表的Pk设置为空全部内容,希望文章能够帮你解决c#-Dapper查询结果在对象中将表的Pk设置为空所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。