NHibernate:错误的列类型:发现浮点,期望双精度 [英] NHibernate: Wrong column type: found float, expected double precision

查看:213
本文介绍了NHibernate:错误的列类型:发现浮点,期望双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有属性的域实体类:

I have a domain entity class with a property:

public virtual double? Result { get; set; }

正在使用NHibernate 3.2按代码映射的东西来映射该属性:

The property is being mapped using the NHibernate 3.2 mapping-by-code stuff:

public class SampleResultMap : ClassMapping<SampleResult>
{
    public SampleResultMap()
    {
        Id(c => c.Id,
            map => map.Generator(Generators.Identity));
        Property(c => c.Result, map =>
        {
            map.NotNullable(false);
        });
        // More properties, etc.
    }
}

这可以正常工作,并且使用数据类型float正确创建了SQL Server 2008 R2表.

This works fine and the SQL Server 2008 R2 table is created properly with a data type of float.

但是,SchemaValidator.Validate调用会出现此错误:

However, the SchemaValidator.Validate call gives this error:

NHibernate.HibernateException was unhandled
Wrong column type in Foo.dbo.SampleResult for column Result.
Found: float, Expected DOUBLE PRECISION

查看对SchemaExport.Create的调用生成的SQL,该表的定义如下:

Looking at the SQL that the call to SchemaExport.Create generates there is this definition for the table:

create table SampleResult (
        Id INT IDENTITY NOT NULL,
       DateEnteredUtc DATETIME not null,
       ElementId INT not null,
       Unit INT not null,
       ResultText NVARCHAR(50) null,
       [Result] DOUBLE PRECISION null,
       Detected BIT not null,
       Qualifier NVARCHAR(10) null,
       SampleId INT not null,
       Deleted BIT not null,
       primary key (Id)
    )

从NHibernate 3.2的快速阅读中可以看出,验证器正在将"DOUBLE PRECISION"和"float"进行比较.

From a quick reading of the NHibernate 3.2 sources it appears that the validator is comparing "DOUBLE PRECISION" to "float".

还有其他人看到过吗?我认为这是一个错误,但是我以前没有使用过验证器,所以想知道我是否做错了什么.

Has anyone else seen this? I assume it is a bug but I haven't used the validator before so wanted to find out if I’m doing something wrong.

推荐答案

我在SQLite数据库上以本机生成的ID遇到了类似的问题,这是由于SQLite仅支持整数列的自动递增而引起的.

I had a similar issue with natively generated IDs on a SQLite DB which was caused because SQLite only supports auto increment on integer columns.

NHibernate已经正确地将ID列创建为整数,但是在对其进行验证时,它认为它应该是int而不是整数.因为int只是在SQLite上映射为整数,所以我刚刚创建了一个新的方言来使用integer而不是int,现在它可以正常工作了.

NHibernate had correctly created the ID column as an integer but when it was validating it, it thought it should be an int instead of an integer. Because int just maps to integer on SQLite I just created a new dialect to use integer instead of int and it now works fine.

由于DOUBLE PRECISION与SQLServer上的FLOAT(53)相同,因此可能是同一回事,它看到的是float而不是double precision.作为解决方法,您可以尝试将方言更改为使用FLOAT(53):

As DOUBLE PRECISION is the same as FLOAT(53) on SQLServer it might be the same thing, it's seeing float instead of double precision. As a work around you could try changing the dialect to use FLOAT(53) instead:

using System.Data;
using NHibernate.Dialect;

public class UpdatedMsSql2008Dialect : MsSql2008Dialect
{
    public UpdatedMsSql2008Dialect()
        : base()
    {
        RegisterColumnType(DbType.Double, "FLOAT(53)");
    }
}

SQLServer方言源似乎表明它应该工作.肯定看起来好像验证程序存在错误.

The SQLServer dialect source seems to suggest it should work. Definitely looks like there is a bug with the validator though.

这篇关于NHibernate:错误的列类型:发现浮点,期望双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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