如何让Dapper将.net datetime映射到datetime2? [英] How can I get Dapper to map .net datetime to datetime2?

查看:179
本文介绍了如何让Dapper将.net datetime映射到datetime2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单,我将现有系统从EF转换为Dapper。由于各种公司原因,我们无法真正更改数据库,某些表的列类型为DateTime2。 Dapper可以将任何.net DateTime转换为DbType.DateTime。

Pretty simple, I'm converting our existing system from EF to Dapper. For various corporate reasons we can't really change the database, some of the tables have columns that are of type DateTime2. Dapper converts any .net DateTime to DbType.DateTime.

一定有人曾对此提出质疑并找到了一个简单的解决方案?

Someone must have bumped against this before and found an easy solution ?

推荐答案

Dapper实际上是包含在代码库中的单个文件。只需编辑文件:

Dapper is litterally a single file that you include into your code base. Just edit the file:

替换(大约300行):

Replace (around line 300):

        typeMap[typeof(Guid)] = DbType.Guid;
        typeMap[typeof(DateTime)] = DbType.DateTime;
        typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset;
        typeMap[typeof(byte[])] = DbType.Binary;

使用:

        typeMap[typeof(Guid)] = DbType.Guid;
        typeMap[typeof(DateTime)] = DbType.DateTime2;
        typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset;
        typeMap[typeof(byte[])] = DbType.Binary;

编辑:

还有一个可为空的DateTime在第319行附近的那个映射块的下方:


There's also a nullable DateTime further down that block of mappings, around line 319:

        typeMap[typeof(DateTime?)] = DbType.DateTime;
        typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;

收件人:

        typeMap[typeof(DateTime?)] = DbType.DateTime2;
        typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;

这篇关于如何让Dapper将.net datetime映射到datetime2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆