dapper 自定义数据库字段和代码中Model字段不一致时候的mapping方法
namespace YourNamespace
{
/// <summary>
/// Uses the Name value of the ColumnAttribute specified, otherwise maps as usual.
/// </summary>
/// <typeparam name="T">The type of the object that this mapper applies to.</typeparam>
public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper
{
public ColumnAttributeTypeMapper()
: base(new SqlMapper.ITypeMap[]
{
new CustomPropertyTypeMap(
typeof(T),
(type, columnName) =>
type.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnMappingAttribute>()
.Any(attr => attr.Name == columnName)
)
),
new DefaultTypeMap(typeof(T))
})
{
}
} [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class ColumnMappingAttribute : Attribute
{
public string Name { get; set; }
} public class FallbackTypeMapper : SqlMapper.ITypeMap
{
private readonly IEnumerable<SqlMapper.ITypeMap> _mappers; public FallbackTypeMapper(IEnumerable<SqlMapper.ITypeMap> mappers)
{
_mappers = mappers;
} public ConstructorInfo FindConstructor(string[] names, Type[] types)
{
foreach (var mapper in _mappers)
{
try
{
ConstructorInfo result = mapper.FindConstructor(names, types);
if (result != null)
{
return result;
}
}
catch (NotImplementedException)
{
}
}
return null;
} public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.GetConstructorParameter(constructor, columnName);
if (result != null)
{
return result;
}
}
catch (NotImplementedException)
{
}
}
return null;
} public SqlMapper.IMemberMap GetMember(string columnName)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.GetMember(columnName);
if (result != null)
{
return result;
}
}
catch (NotImplementedException)
{
}
}
return null;
}
} }
public static IEnumerable LoadWithCustomMapping()
{
using (var conn = OpenDBConnection())
{
Dapper.SqlMapper.SetTypeMap(typeof(MyArea), new ColumnAttributeTypeMapper());
return conn.Query("SELECT TOP 10 CAID,CAName,En FROM CityArea");
}
}
dapper 自定义数据库字段和代码中Model字段不一致时候的mapping方法的更多相关文章
- 【Java/JDBC】利用ResultSetMetaData从数据库的某表中获取字段信息并存到csv文件
代码下载:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-2.rar 这篇还不够完善,请看更完善的续篇 ...
- 用mybatis做数据库处理 代码中的字段大小写 要和mapper映射设置的大小写一致(这TM不废话么,原谅我渣!~~)
简单描述情况:其实这个问题怎么说呢,有人给你说,你肯定能意识到,必须大小写对应的嘛.emmmm我现在才意识到是因为:自己在下边敲代码,配的mapper文件并没有把属性变量,和数据库里的段单独提出来做映 ...
- 实现Django ORM admin view中model字段choices取值自动更新的一种方法
有两个表,一个是记录网站信息的site表,结构如下: CREATE TABLE `site` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `name` ) N ...
- django中model字段与属性
model field 类型1.AutoField 一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key. 2.BigIntege ...
- Android 代码中文字在手机上显示乱码问题解决方法
在学习Android过程中,用于测试时发现,代码中的中文在真机上会显示乱码, 网上查阅了些资料,参考如下: http://www.androidchina.net/3024.html http://b ...
- 【转】VS2008快速将代码中字符串改为_T(“”)风格的方法
用VC在修改一些老程序的时候,经常面临“UNICODE化”的工作.就是将一些传统C语言风格的字符串,如“string”,改为既能够通过多字节编码工程编译,又能通过UNICODE工程编译的代码,即形如_ ...
- 获取Django中model字段名 字段的verbose_name
obj._meta.fields 为关键 obj为model类 推荐使用函数 from django.apps import apps def getmodelfield(appname,modeln ...
- 用xib自定义UIView并在代码中使用--iOS
首先新建一个空的xib文件: 将size改为freedom: 然后在xib中自定义视图(添加自己想要的其它视图): 要写好约束, 创建一个继承uiview的类和他关联,然后就可以调用了. - (voi ...
- Django源代码中model字段
to_field='绑定的主键字段(默认为ID)' limit_choices_to = {'过滤的choices字段':choices的ID} related_name = 关联表反向查找自己的名称 ...
随机推荐
- Debian下的PPPOE服务器配置
参考: http://blog.csdn.net/zhangwenjianqin/article/details/7655375 http://blog.sina.com.cn/s/blog_8043 ...
- CentOS下通过locale来设置字符集
转载:http://www.centoscn.com/CentOS/config/2013/1013/1784.html 在Centos中通过locale来设置程序运行的不同语言环境,locale由A ...
- css预处理的引入与问题
css的预处理越来越流行.sass,less,stylus这几个都使用方便. 我想使用他的原因,暂时最主要是为了@import的功能.现在的问题:因为产品太多,是个页面,有10个css.而这10个cs ...
- ArcGIS动态文本
处理动态文本 来自:http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#/na/00s900000013000000/ Deskto ...
- GCD 的使用
// // ZYGCDViewController.h // Thread // // Created by wanglixing on 14/11/4. // Copyright © 2014年 z ...
- VbCrlf的相关说明
今天从别人发来的手顺书中发现了这几个字母,一头雾水,下面查了一下相关资料. vbCrLf是Visual Basic中的一个字符串常数,即“Chr(13) & Chr(10)”(回车符与换行符连 ...
- hdu 1358 KMP的next数据运用
由于next[i]保存的是前i-1个字符中最大的重复子序列,那么i-next[i]就是循环节. #include<cstdio> #include<cstring> #incl ...
- Android第三方授权(新浪微博篇)
Android第三方认证新浪微博,相对微信,也比较简单,并且sina给了一个sdk和sdkdemo,这个demo封装了许多,但是自己不准备记录这个demo,而是直接使用sdk 同样去sina官方下载s ...
- js解析json读取List中的实体对象示例
1.由后台action 传给前台是需要将map 转成json格式 复制代码代码如下: Map<String, List> resultMap: JSONObject json = JSON ...
- DOS批处理命令-pause
“pause”用于暂停批处理的执行器并给出提示信息,然后由用户决定是继续执行还是终端执行. 语法: pause 简单一个pause,不附带任何参数. 执行pause以后会提示一句话[按任意键继续执行] ...