SubSonic:MySql、Float 和 Single-> Decimal 问题
有2个MySQL数据库。一个是主数据库,另一个用于地理定位数据。现在,为了让 SubSonic 通过 Subcommander 很好地处理生成的文件,我简化了操作,只需创建一个映射到另一个数据库中的地理位置数据的视图(这样,所有表和地理位置数据在技术上都位于一个数据库中)。
现在,我遇到的问题是: 在地理位置表中,有 2 个字段(纬度、经度),它们都是浮点型。
当我运行标准 SubSonic 语句来获取数据时:
return new Select()
.From(ZipDatum.Schema)
.Where(ZipDatum.Columns.Zipcode).IsEqualTo(zipCode)
.ExecuteSingle<ZipDatum>();
我在测试项目中遇到此异常: “TestCanGetZipData”失败:System.ArgumentException:“System.Single”类型的对象无法转换为“System.Decimal”类型。
看起来 SubSonic 将浮点字段比作十进制。但这个异常让我陷入了困境。有办法解决这个问题吗? FWIW,在所有其他 200 多个表中,我们都使用十进制作为需要此类的字段。但由于这是第三方数据库表,他们使用的是浮动,这会导致问题。
有人遇到过这种情况吗?
Have 2 MySQL databases. One is the main database, the other is used for geolocation data. Now, for SubSonic to play nice with the generated files via Subcommander, I made it easy and just created a view that maps to the geolocation data in the other database (that way all the tables and geolocation data is technically in one database).
Now, the issue I'm running into is this:
In the geolocation table, there are 2 fields (latitude, longtitude) which are both floats.
When I run your standard SubSonic statement to get the data:
return new Select()
.From(ZipDatum.Schema)
.Where(ZipDatum.Columns.Zipcode).IsEqualTo(zipCode)
.ExecuteSingle<ZipDatum>();
I get this exception in my tests project:
'TestCanGetZipData' failed: System.ArgumentException : Object of type 'System.Single' cannot be converted to type 'System.Decimal'.
It looks like SubSonic likens float fields to decimal. But this exception is throwing me for a loop. Any way to get around this? FWIW, on all of the other 200+ tables we're using decimal for our fields that require such. But since this is a 3rd party database table, they're using float and it's causing issues.
Anyone run into this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是使用 ExecuteTypedList 方法。
您必须创建一个 DTO 类:
将您的查询重写为
ExecuteTypedList 方法是一种非常通用的方法,因为它尝试将 DataReader 中的列与类的属性进行匹配。
它非常具体,因为它
区分大小写
b 您的属性必须与 DataReader 返回的系统类型完全匹配,
只需让您的测试运行并修改结果,直到没有任何错误为止。
另一个解决方案是修改 SubSonic 源来为视图 mysql 类型生成系统类型 single。
https://github.com/subsonic/SubSonic-2.0 /blob/master/SubSonic/DataProviders/MySqlDataProvider.cs
从mysqltype获取DbType(十进制是为decimal、float、newdecimal、numeric、double、real创建的)
您必须弄清楚这里哪一个是错误的,然后返回 DbType.Single 。
https://github.com/subsonic/SubSonic-2.0/blob /master/SubSonic/Utility.cs
仅供参考:获取指定 DbType 的 System.Type。你不应该修改它。
One solution would be to use the ExecuteTypedList method.
You have to create a DTO class:
rewrite your Query to
The ExecuteTypedList method is a very generic approach since it tries to match columns from a DataReader to properties of your class.
It is very specific since it is
a Case Sensitive
b your properties have to match exactly the system type retured by the DataReader
Just let your test run and modify the result until you don't get any errors.
Another solution would be to modify the SubSonic source to generate system type single for the views mysql type.
https://github.com/subsonic/SubSonic-2.0/blob/master/SubSonic/DataProviders/MySqlDataProvider.cs
get's the DbType from mysqltype (decimal is created for decimal, float, newdecimal, numeric, double, real)
You have to figure out which one is wrong here and return DbType.Single instead.
https://github.com/subsonic/SubSonic-2.0/blob/master/SubSonic/Utility.cs
FYI: get's the System.Type for the specified DbType. You shouldn't modify that.