Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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# NHibernate不清除连续异常消息中的SQL Server错误文本_C#_.net_Sql Server_Nhibernate - Fatal编程技术网

C# NHibernate不清除连续异常消息中的SQL Server错误文本

C# NHibernate不清除连续异常消息中的SQL Server错误文本,c#,.net,sql-server,nhibernate,C#,.net,Sql Server,Nhibernate,我使用这段代码(简化)来测试一些异常和错误处理: // All the objects in the collection already exist in the database // so NHibernate should throw an Exception because of a PK constraint // violation foreach (var existingEntity in existingEntities) { // Implementation ca

我使用这段代码(简化)来测试一些异常和错误处理:

// All the objects in the collection already exist in the database
// so NHibernate should throw an Exception because of a PK constraint
// violation
foreach (var existingEntity in existingEntities)
{
    // Implementation calls Session.CreateTransaction(), 
    // but the interface returns System.Data.IDbTransaction.
    // This assembly has no references to NHibernate.
    using (var transaction = repo.CreateTransaction())
    {
        try
        {
            // Executes a simple insert
            repo.SaveComposite(existingEntity);
            transaction.Commit();
        }
        catch (Exception e)
        {
            transaction.Rollback();

            // Only record the inner-most message
            var innerException = e;
            while (innerException.InnerException != null)
                innerException = innerException.InnerException;

            Errors.Add(new Error(ErrorCode.ExceptionOccurred, innerException.Message));
        }
    }
}
问题在于,每个连续异常都包含前一个异常的错误文本

例如,当尝试插入3个具有现有ID 1、2和3的实体时,将生成以下InnerException消息:

ID为1的项目:

违反主键约束“PK_示例”。无法插入 对象“dbo.Example”中存在重复的键。重复的键值为 (1) \r\n语句已终止。“

ID为2的项目:

违反主键约束“PK_示例”。无法插入 对象“dbo.Example”中存在重复的键。重复的键值为 (1) \r\n违反主键约束“PK_示例”。无法 在对象“dbo.Example”中插入重复的键。重复的键值 是(2)。\r\n语句已终止。\r\n语句已终止 已被终止。”

ID为3的项目:

违反主键约束“PK_示例”。无法插入 对象“dbo.Example”中存在重复的键。重复的键值为 (1) \r\n违反主键约束“PK_示例”。无法 在对象“dbo.Example”中插入重复的键。重复的键值 是(2)。\r\n违反主键约束“PK\u示例”。无法 在对象“dbo.Example”中插入重复的键。重复的键值 是(3)。\r\n语句已终止。\r\n语句已终止 已终止。\r\n语句已终止。“

这是SQL Server还是NHibernate问题?如何强制NHibernate清除以前的错误消息

[编辑]:如果我将批次大小设置为1(当前使用默认批次大小),则每次迭代都会重复并嵌套相同的错误消息


调用Session.Clear()后,问题仍然存在。

通常情况下,当遇到异常时,您不希望重用ISession。应在异常发生后处理ISession

我还要提到,我不确定这些错误到底是如何复制的。您提到了特定的输出,但它是由以下语句输出的吗

Errors.Add(new Error(ErrorCode.ExceptionOccurred, innerException.Message));
如果您在将每个错误添加到“集合”时记录或输出这些错误,您将获得上面描述的输出。您可能希望等到foreach循环结束后再输出添加到
错误中的任何内容。您可能希望在调试sql时观察sql的运行,以验证我所说的内容

我还要提到,如果您试图保存可能存在或不存在的实体,您可能希望使用ISession.Merge,而不是您当前使用的实体

从:

此方法将给定对象的状态复制到具有相同标识符的持久对象上。如果当前没有与会话关联的持久实例,则将加载该实例。该方法返回持久实例。如果给定实例未保存或不存在于数据库中,NHibernate将保存该实例并将其作为新的持久实例返回。否则,给定实例不会与会话关联。在大多数具有分离对象的应用程序中,都需要SaveOrUpdate()和Merge()两种方法


是否在映射中指定了某些批处理大小?否。映射仅指定列/对象名称映射和键关系。错误消息直接取自“调试”视图中的异常。突出显示的代码只会将消息添加到外部列表中。此外,所讨论的代码永远不会执行更新或合并。它专门用于插入。@ILITRIT如果这是真的,那么很可能是在每次错误发生后使用相同的ISession。同样,一般不建议这样做。在出现错误后,您可以调用
ISession.Clear()
删除与所讨论的
ISession
相关的任何现有更新,但同样不建议这样做。我已尝试清除会话,但没有效果。问题是这段代码在Web控制器上运行,会话在每个请求开始时创建,注入到存储库中,然后处理,所以我无法控制它。