NHibernate Restrictions.Ge(“ColumnName”,DateTime.Now)生成无效的SQL

时间:2012-03-15 15:29:08

标签: nhibernate fluent-nhibernate sql-server-2008-r2

对于DateTime列,我有这样的映射:

...
Map(x => x.Created).Column("CREATED")
                   .Access.Property()
                   .CustomType<DateTime>()
                   .CustomSqlType("datetime")
                   .Not.Nullable();
...

在我的代码中我定义了

outboxCriteria.Add(Restrictions.Eq("Created", startDate));

其中'startDate'的类型为DateTime。

查看创建的SQL时,我看到了上述标准

...
    and Created = 2/14/2012 12:00:00 AM
...

这是不正确的。我希望NHibernate能够创建

...
    and Created = '2/14/2012 12:00:00 AM'
...

我也观察到String类型存在同样的问题。

Map( x => x.ReceiverName).Column("UserName")
                         .CustomType("string")
                         .Access.Property()
                         .CustomSqlType("nvarchar(256)")
                         .Nullable()
                         .Length(256);

生成的SQL不会将字符串放在引号中:

...
    and UserName = Paul
...

而不是

...
    and UserName = 'Paul'
...

除了这些问题,映射效果很好。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

指定.CustomSqlType("nvarchar(256)")Length()渲染为noop。 string和datetime也不是customeTypes / customSqlTypes。也许NHibernate对它们感到困惑。

删除Map( x => x.ReceiverName).Column("UserName").Length(256)以外的所有内容。