如何让 Dapper 在映射时在字段名称中使用 ignore/remove 下划线?
How to get Dapper to ignore/remove underscores in field names when mapping?
将数据库字段名称映射到 class 名称的方法有很多种,但是仅删除下划线的最简单方法是什么?
public IEnumerable<PersonResult> GetPerson(int personId)
{
using (var dbConnection = _dbConnectionFactory.Create(ConnectionStrings.ProjectXYZ))
{
IEnumerable<PersonResult> result =
dbConnection.Query<PersonResult>("fn_get_person", new { personId },
commandType: CommandType.StoredProcedure).ToList();
return result;
}
}
Table 和数据库字段:
person
--------
person_id
full_name
Class 有效:(dapper 已经忽略了大写)
public class PersonResult
{
public int Person_Id { get; set; }
public string Full_Name { get; set; }
}
我想将 class 更改为:
public class PersonResult
{
public int PersonId { get; set; }
public string FullName { get; set; }
}
readme 不支持重命名列。您可以在 select
:
中为它们添加别名
select person_id as personid, full_name as fullname from fn_get_person();
如 this answer 中所建议。
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
任务完成;p
将数据库字段名称映射到 class 名称的方法有很多种,但是仅删除下划线的最简单方法是什么?
public IEnumerable<PersonResult> GetPerson(int personId)
{
using (var dbConnection = _dbConnectionFactory.Create(ConnectionStrings.ProjectXYZ))
{
IEnumerable<PersonResult> result =
dbConnection.Query<PersonResult>("fn_get_person", new { personId },
commandType: CommandType.StoredProcedure).ToList();
return result;
}
}
Table 和数据库字段:
person
--------
person_id
full_name
Class 有效:(dapper 已经忽略了大写)
public class PersonResult
{
public int Person_Id { get; set; }
public string Full_Name { get; set; }
}
我想将 class 更改为:
public class PersonResult
{
public int PersonId { get; set; }
public string FullName { get; set; }
}
readme 不支持重命名列。您可以在 select
:
select person_id as personid, full_name as fullname from fn_get_person();
如 this answer 中所建议。
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
任务完成;p