C# 从 NHibernate 获取 {“无法执行批处理命令。[SQL:SQL 不可用]”} 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9482784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting {"could not execute batch command.[SQL: SQL not available]"} error from NHibernate
提问by Javid_p84
I took a look at other related posts but couldn't find any solution.
Sometimes on sesstion.Flush() I get the following error:
我查看了其他相关帖子,但找不到任何解决方案。
有时在 sesstion.Flush() 上我收到以下错误:
{"could not execute batch command.[SQL: SQL not available]"}
{“无法执行批处理命令。[SQL:SQL 不可用]”}
and the Inner Exception :
和内部异常:
{"The UPDATE statement conflicted with the FOREIGN KEY constraint FK1377052553ABF955. The conflict occurred in database ProcessDebug, table dbo.Adjustment, column 'AdjustmentId'.The statement has been terminated."}
{“UPDATE 语句与 FOREIGN KEY 约束 FK1377052553ABF955 冲突。冲突发生在数据库 ProcessDebug,表 dbo.Adjustment,列 'AdjustmentId'。该语句已终止。”}
a piece of Process class mapping :
一块 Process 类映射:
References(p => p.CurrentAdjustment)
;
References(p => p.DefaultAdjustment)
;
HasMany(p => p.Adjustments)
.Cascade.AllDeleteOrphan()
.Inverse()
;
All these properties above are of type of Adjustment. As long as I get this error once in a while I couldn't track it down. For an entity it might happen now, but not next time in a same piece of code....
以上所有这些属性都是调整类型。只要我偶尔收到这个错误,我就无法追踪它。对于一个实体,它现在可能会发生,但下次不会发生在同一段代码中......
Any idea what might cause the problem?
I'm using NH 3.2 and FluentNhibernate
Thanks in advance
知道什么可能导致问题吗?
我正在使用 NH 3.2 和 FluentNhibernate
提前致谢
回答by Cole W
You need to look at the sql that is actually trying to execute.
您需要查看实际尝试执行的 sql。
It appears as though you are trying to update the primary key ("AdjustmentId") to something that does not exist. Hence the foreign key violation.
看起来好像您正在尝试将主键(“AdjustmentId”)更新为不存在的内容。因此,外键违规。
回答by u1230329
it seems about you database, not your nHibernate codes, check the SQL in log file, and try to exec it
似乎是关于您的数据库,而不是您的 nHibernate 代码,请检查日志文件中的 SQL,然后尝试执行它
回答by zchpit
In my situation, it was "NULL" in one of the databese columns. Check your database data.
在我的情况下,它在数据库列之一中为“NULL”。检查您的数据库数据。
FOREIGN KEY -> this means, that you propably have null i column, that is use for "join".
FOREIGN KEY -> 这意味着,您可能有空 i 列,用于“加入”。
p.s. Take SQL Profiler and check the SQL generated by nHibernate.
ps 使用 SQL Profiler 并检查 nHibernate 生成的 SQL。
回答by yu yang Jian
In my case I'm adding a data with column whose type is DateTime,
I insert 2100/2/2get this error, but when insert more reasonable time 2001/2/2get success,
在我的例子中,我添加了一个类型为 DateTime 的列的数据,我插入2100/2/2得到这个错误,但是当插入更合理的时间2001/2/2获得成功时,
so maybe the problem is in your data, and should follow some rule in database.
所以也许问题出在你的数据中,应该遵循数据库中的一些规则。
回答by Ashraf Alam
To find the actual cause, you'll need to see the SQL that is being generated by nHibernate. You can either use nHibernate log or nHibernate profiler to get these SQL. Few of the common issues related to the above error message, include:
要找到真正的原因,您需要查看 nHibernate 生成的 SQL。您可以使用 nHibernate log 或 nHibernate profiler 来获取这些 SQL。与上述错误消息相关的一些常见问题包括:
- String or binary data would be truncated. An example of this issue is whenever you provide a string value that is larger than the defined varchar/nvarchar field size in database.
- Expected primary or foreign key value is null
- 字符串或二进制数据将被截断。此问题的一个示例是,只要您提供的字符串值大于数据库中定义的 varchar/nvarchar 字段大小。
- 预期的主键或外键值为空
回答by Amran Hossain
may be its not NHibernate problem! please check in database which column is Not Null/Null. For this,I set null those columns. You can do anything! set value when insert or set null on those column on table.
可能不是NHibernate的问题!请检查数据库哪个列不是空/空。为此,我将这些列设置为空。你可以做任何事情!在表上的那些列上插入或设置空值时设置值。

