如何解决“批量更新从更新中返回意外行数;”的问题实际行数:0;预期:1”问题?

发布于 2024-09-30 08:44:48 字数 280 浏览 0 评论 0原文

每次我尝试创建一个特定实体时都会得到这个......只是想知道我应该如何找出原因。

我正在使用 Fluent NHibernate 自动映射,因此也许我没有适当地设置约定和/或需要覆盖一个或多个映射文件中的某些内容。我浏览了网络上有关此问题的许多帖子,但很难弄清楚为什么会在我的案例中发生这种情况。

我要保存的对象非常简单。它是一个“Person”对象,引用“Company”实体并具有“Address”实体的集合。 UPDATES 在数据库中已有的现有 Person 对象上工作得很好。

有什么建议吗?

Getting this every time I attempt to CREATE a particular entity ... just want to know how I should go about figuring out the cause.

I'm using Fluent NHibernate auto-mapping so perhaps I haven't set a convention appropriately and/or need to override somethings in one or more mapping files. I've gone thru a number of posts on the web regarding this problem and having a hard time figuring out exactly why it is happening in my case.

The object I'm saving is pretty simple. It is a "Person" object that references a "Company" entity and has a collection of "Address" entities. UPDATES work fine on existing Person objects that are already in the database.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

滴情不沾 2024-10-07 08:44:48

该错误表示正在执行 SQL INSERT 语句,但 SQL Server 运行后返回的 ROWCOUNT 为 0,而不是预期的 1。

有多种原因,从不正确的映射到关闭行计数的 UPDATE/INSERT 触发器。

最好的办法是分析 SQL 语句并看看会发生什么。为此,请打开 nHibernate sql 日志记录,或使用sql 分析器。一旦你有了 SQL,你可能就会知道原因,如果没有,请尝试手动运行 SQL,看看会发生什么。

另外,我建议您发布您的映射,因为它将帮助人们发现任何问题。

The error means that the SQL INSERT statement is being executed, but the ROWCOUNT being returned by SQL Server after it runs is 0, not 1 as expected.

There are several causes, from incorrect mappings, to UPDATE/INSERT triggers that have rowcount turned off.

Your best beat is to profile the SQL statements and see what happens. To do that either turn on nHibernate sql logging, or use the sql profiler. Once you have the SQL you may know the cause, if not try running the SQL manually and see what happens.

Also I suggest you to post your mapping, as it will help people spot any issues.

左岸枫 2024-10-07 08:44:48

当触发器执行影响行计数的额外 DML(数据修改)查询时,可能会发生这种情况。我的解决方案是在触发器顶部添加以下内容:

SET NOCOUNT ON;

This can happen when trigger(s) execute additional DML (data modification) queries which affect the row counts. My solution was to add the following at the top of my trigger:

SET NOCOUNT ON;
源来凯始玺欢你 2024-10-07 08:44:48

这可能是由于自动增量主键而发生的。为了解决这个问题,不要在数据集中插入自动增量值。插入没有主键的数据。

This may occur because of Auto increment primary key. To solve this problem do not inset auto increment value with data set. Insert data without the primary key.

十年不长 2024-10-07 08:44:48

当使用 INSTEAD OF 触发器定位视图时,几乎不可能获得正确的行数。在深入研究源代码后,我发现您可以创建一个自定义持久器,使 NHibernate 忽略计数检查。

public class SingleTableNoResultCheckEntityPersister : SingleTableEntityPersister
{
    public SingleTableNoResultCheckEntityPersister(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
        : base(persistentClass, cache, factory, mapping)
    {
        for (int i = 0; i < this.insertResultCheckStyles.Length; i++)
        {
            this.insertResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }

        for (int i = 0; i < this.updateResultCheckStyles.Length; i++)
        {
            this.updateResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }

        for (int i = 0; i < this.deleteResultCheckStyles.Length; i++)
        {
            this.deleteResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }
    }
}

When targeting a view with an INSTEAD OF trigger it can be next to impossible to get the correct row count. After delving a bit into the source I found out that you can make a custom persister which makes NHibernate ignore the count checks.

public class SingleTableNoResultCheckEntityPersister : SingleTableEntityPersister
{
    public SingleTableNoResultCheckEntityPersister(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
        : base(persistentClass, cache, factory, mapping)
    {
        for (int i = 0; i < this.insertResultCheckStyles.Length; i++)
        {
            this.insertResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }

        for (int i = 0; i < this.updateResultCheckStyles.Length; i++)
        {
            this.updateResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }

        for (int i = 0; i < this.deleteResultCheckStyles.Length; i++)
        {
            this.deleteResultCheckStyles[i] = ExecuteUpdateResultCheckStyle.None;
        }
    }
}
但可醉心 2024-10-07 08:44:48

如果行数大于 1,通常是因为表中存在具有相同 id 的重复行。检查表中是否有重复的记录。删除重复项即可解决该问题。

NHibernate.AdoNet.TooManyRowsAffectedException:'返回批量更新
更新中意外的行数;实际行数:2;预期:1'

if you get row count greater than 1 its usually because you have duplicate rows in a table that have same ids. Check your table for duplicated records. Deleting duplicates will fix it.

NHibernate.AdoNet.TooManyRowsAffectedException: 'Batch update returned
unexpected row count from update; actual row count: 2; expected: 1'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文