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方法的更多相关文章

  1. 【Java/JDBC】利用ResultSetMetaData从数据库的某表中获取字段信息并存到csv文件

    代码下载:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-2.rar 这篇还不够完善,请看更完善的续篇 ...

  2. 用mybatis做数据库处理 代码中的字段大小写 要和mapper映射设置的大小写一致(这TM不废话么,原谅我渣!~~)

    简单描述情况:其实这个问题怎么说呢,有人给你说,你肯定能意识到,必须大小写对应的嘛.emmmm我现在才意识到是因为:自己在下边敲代码,配的mapper文件并没有把属性变量,和数据库里的段单独提出来做映 ...

  3. 实现Django ORM admin view中model字段choices取值自动更新的一种方法

    有两个表,一个是记录网站信息的site表,结构如下: CREATE TABLE `site` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `name` ) N ...

  4. django中model字段与属性

    model field 类型1.AutoField     一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key. 2.BigIntege ...

  5. Android 代码中文字在手机上显示乱码问题解决方法

    在学习Android过程中,用于测试时发现,代码中的中文在真机上会显示乱码, 网上查阅了些资料,参考如下: http://www.androidchina.net/3024.html http://b ...

  6. 【转】VS2008快速将代码中字符串改为_T(“”)风格的方法

    用VC在修改一些老程序的时候,经常面临“UNICODE化”的工作.就是将一些传统C语言风格的字符串,如“string”,改为既能够通过多字节编码工程编译,又能通过UNICODE工程编译的代码,即形如_ ...

  7. 获取Django中model字段名 字段的verbose_name

    obj._meta.fields 为关键 obj为model类 推荐使用函数 from django.apps import apps def getmodelfield(appname,modeln ...

  8. 用xib自定义UIView并在代码中使用--iOS

    首先新建一个空的xib文件: 将size改为freedom: 然后在xib中自定义视图(添加自己想要的其它视图): 要写好约束, 创建一个继承uiview的类和他关联,然后就可以调用了. - (voi ...

  9. Django源代码中model字段

    to_field='绑定的主键字段(默认为ID)' limit_choices_to = {'过滤的choices字段':choices的ID} related_name = 关联表反向查找自己的名称 ...

随机推荐

  1. Shlwapi.h Shlwapi.dll 动态库

    Windows中有一个Shlwapi.dll文件,包含了大量的Windows字符串处理方法,这些方法,在通常的程序应用中,经常会用到,有一部分处理方法,在CRuntime中也存在,但不方便使用.有一部 ...

  2. Http Statis 500 -错误笔记

    HTTP Status 500 - type Exception report message description The server encountered an internal error ...

  3. Java垃圾回收(GC)与引用的种类

    垃圾回收 GC public class MyObject {     private String id;     public MyObject(String id) {         this ...

  4. 对于android触摸事件模型的一些理解

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  5. python(1) - 输入和输出

    前面已经说过了,print()函数括号里加上字符串,就可以实现输出 >>> print('This is Python!') This is Python! print()函数也可以 ...

  6. CentOS(三)--初识linux的文件系统以及用户组等概念

    进入到了Linux学习之CentOS第三篇了,这篇文章主要记录下对linux文件系统的初步认识,以及用户组.用户权限.文件所有者.文件所在组等概念 一.Linux文件结构及基本文件夹 文件系统是Lin ...

  7. 程氏CMS去掉静态页面的隐藏性版权方法

    程氏CMS去掉静态页面的隐藏性版权方法 实例如图: 因为之前自己找了好久都没找到这俩代码写在那个文件夹的,经过跟csqq8讨论了也没有得到结果,今天突然发现,原来这些代码都经过base64加密,用加密 ...

  8. LeetCode 344

    Reverse String Write a function that takes a string as input and returns the string reversed. Exampl ...

  9. mac端口占用查找进程并杀死

    查找端口port被占用的进程 lsof -i tcp:port 杀死进程pid kill -9 pid

  10. SQL Server 2005中约束

    在SQL Server 2005中有6种约束:主键约束(primary key constraint).惟一性约束(unique constraint).检查约束(check constraint). ...