Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将日期类型的命名参数插入HQL Insert_C#_Nhibernate_Hql - Fatal编程技术网

C# 将日期类型的命名参数插入HQL Insert

C# 将日期类型的命名参数插入HQL Insert,c#,nhibernate,hql,C#,Nhibernate,Hql,我试图执行一个HQL insert语句,将一些参数插入到映射到实体的表中 以下是我目前正在处理的内容(引发异常): 如果我省略了两个日期字段,插入就起作用了,所以我知道我已经接近了。我只需要弄清楚如何让nhibernate将我的日期视为日期 例外情况: Insert Into Entity1 (ForeignKey1, ForeignKey2, Date1, Date2) SELECT this_.ForeignKey1, cast(:param1 as int) as For

我试图执行一个HQL insert语句,将一些参数插入到映射到实体的表中

以下是我目前正在处理的内容(引发异常):

如果我省略了两个日期字段,插入就起作用了,所以我知道我已经接近了。我只需要弄清楚如何让nhibernate将我的日期视为日期

例外情况:

Insert Into Entity1
(ForeignKey1, ForeignKey2, Date1, Date2)

SELECT this_.ForeignKey1, 
       cast(:param1 as int) as ForeignKeyValue2, 
       cast(:param2 as DateTime) as DateValue1, 
       cast(:param3 as DateTime) as DateValue2
FROM OtherEntity this_
WHERE ...
insertion type [NHibernate.Type.Int32Type] and selection type  
[NHibernate.Dialect.Function.CastFunction+LazyType] at position 1 are not compatible
[Insert Into Entity1
(ForeignKey1, ForeignKey2, Date1, Date2)

SELECT this_.ForeignKey1, 
       cast(:param1 as int) as ForeignKeyValue2, 
       cast(:param2 as DateTime) as DateValue1, 
       cast(:param3 as DateTime) as DateValue2
FROM OtherEntity this_
WHERE ...

如果有人有做这类事情的经验,请随时提供帮助。另外,如果您知道如何在DateTimes上使用HQL cast,也会有所帮助。DBMS是MS SQL 2008。

您不需要强制转换,只需将参数设置为正确的类型,例如

var hql = "insert into Entity1 (fkid, mydate) 
  select fkid, :date from OtherEntity";

session.CreateQuery(hql)
  .setDateTime("date", DateTime.Now)
  .ExecuteUpdate();

为什么使用SQL强制转换?为什么不直接从NH调用中发送正确的参数类型呢?我们能看到设置参数的代码吗?尝试这个会给我一个空引用异常。假设这真的适用于您,我将尝试更新我的nhibernate DLL,因为我仍在使用2.1.2。我将公布结果。感谢发布a)id和日期字段的数据库结构,2)实际异常(内部异常)没有内部异常(超级有用的权利…)。这只是调用session.CreateQuery(myQueryString)时引发的空引用异常。身份证不是问题。就像我说的,如果我去掉日期参数,插入的工作方式和我原来的一样。该表上的日期字段可以为空,因此我可以在不设置它们的情况下插入(是的,我确保在设置参数时传入实际日期)。我仍在努力更新DLL。如果我成功了,我会告诉你的。你确定a)会话不是空的,b)空引用异常不是因为其他参数/属性等设置正确。HQL应该和V2.2一样好。是的,我确定。我也在使用V2.1。将nhibernate的Dll更新为3.2,现在可以使用您在答案中建议的代码工作。谢谢你的帮助!