更改小巧玲珑,以便其映射数据库空值double.NaN [英] Change Dapper so that it maps a database null value to double.NaN

查看:185
本文介绍了更改小巧玲珑,以便其映射数据库空值double.NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的SQLite数据库可为空的双列。

I have a nullable double column in my SQLite database.

从数据库中读取数据时(double类型的列),我想空值转换成double.NaN。

When reading from the database (for columns of type double) I would like to convert nulls into "double.NaN".

目前短小精悍套空值设置为0,这是我不想要的。

Currently dapper sets null values to 0, which I do not want.

我有什么选择?

  1. 修改小巧玲珑源$ C ​​$ C。
  2. 在无法使用小巧玲珑,需要写我自己的ADO.NET code的老式方法?
  3. 在改变我称之为cnn.Query法的方式,以修改方式映射发生。

我的第一选择是选项1,但我需要帮助修改小巧玲珑。

My first choice is option 1, but I need help modifying Dapper.

推荐答案

就个人而言,我会反对这项建议;一个空的,并不完全是一回事NaN的。如果你真的想这样做,你将不得不看看 GetTypeDeserializer 。在code做,这是动态生成使用的ILGenerator ,而且是相当复杂的。如果你在寻找一个行:

Personally, I will advise against this; a null is not quite the same thing as NaN. If you really want to do this, you would have to look at GetTypeDeserializer. The code to do this is generated dynamically using ILGenerator, and is fairly complex. If you look for a line:

il.MarkLabel(isDbNullLabel); // incoming stack: [target][target][value]

这是其中code分支,如果的DBNull 被检测到。它的作用是目前简单地从栈中弹出两个值(该值和目标),把它们放在地板上,而矣。您需要检查浮动 / 作为一个特例,适用您的转换,然后分配楠到成员。

this is where the code branches to if a DbNull is detected. What it does currently is simply pop the two values (the value and the target) from the stack, drop them on the floor, and carry on. You would need to check for float/double as a special case, apply your conversion, then assign the NaN to the member.

我重申我的要求,但是,这根本就不是一个有效的事情。一个更简单的办法是:

I repeat my claim, though, that this simply isn't a valid thing to do. A much simpler option would be:

public double? Value {get;set;}

这需要零的变化,并且将当前的工作。如果你真的真的想把它当作不可空双,也许是:

which requires zero changes, and will work currently. If you really really want it treated as a non-nullable double, maybe:

private double foo = double.NaN;
public double Foo { get { return foo; } set { foo = value; } }

现在,这将默认为 NaN的,正确兑现的时候有值。

this will now default to NaN, and materialize correctly when there are values.

这篇关于更改小巧玲珑,以便其映射数据库空值double.NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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