简单语句上未找到或更改行 LINQ C# 错误

发布于 2024-09-30 22:57:42 字数 2165 浏览 3 评论 0原文

首先,这不可能是多用户问题,因为我正在本地处理数据库的开发版本。

当我执行 db.SubmitChanges() 时,我收到了抛出的解释性不强的 Row not find orchange 错误。如果我在 SubmitChanges() 发生之前中断执行,我可以在 SQL Server Management Studio 中检查,并且行确实存在!

这是整个函数的代码,只是为了将其放在上下文中以供任何想要提供帮助的人使用,但问题行就在最后(第 48 行)。

更新 这确实很奇怪:错误是由更新matchingTrans.Url 引起的(参见倒数第二行代码)。注释掉这一行不会引发错误 - 即使matchingTrans.Title 仍然会更新。

private static void MenuItemUpdate(int languageId, NavigationItem item)
{
    using (var db = DataContextFactory.Create<MyDataContext>())
    {
        // Select existing menu item from database.
        var dbItem =
            (from i in db.MenuItems
             where i.Id == item.Id
             select i).Single();
        // Obtain ID of link type.
        dbItem.FkLinkTypeId = GetLinkTypeByName(
            Enum.GetName(typeof (NavigationItemLinkType), item.LinkType)).Id;
        // Update the Link field with what is given.
        dbItem.Link = item.Link;
        db.SubmitChanges();

        // Item already exists and needs editing.
        // Get associated translations.
        var trans =
            from t in db.MenuItemTranslations
            where t.FkMenuItemId == item.Id
            select t;

        // If translation exists for given language, edit it.
        var matchingTrans =
            (from t in trans
             where t.FkLanguageId == languageId
             select t).SingleOrDefault();

        if (matchingTrans == null)
        {
            // No matching translation - add one.
            var newDbTrans = new MenuItemTranslation
            {
                FkMenuItemId = item.Id,
                FkLanguageId = languageId,
                Title = item.Title,
                Url = item.FriendlyUrl
            };
            db.MenuItemTranslations.InsertOnSubmit(newDbTrans);
            db.SubmitChanges();
        }
        else
        {
            // Matching translation - edit it.
            matchingTrans.Title = item.Title;
            matchingTrans.Url = item.FriendlyUrl;
            db.SubmitChanges();
            // WTF ERROR: Row not found or changed.
        }
    }
}

First of all, there is no chance that this is a multi-user issue, as I'm working locally on a dev version of the database.

I am getting the not very explanatory Row not found or changed error being thrown when I perform db.SubmitChanges(). If I break the execution just before the SubmitChanges() occurs, I can check in SQL Server Management Studio and the row does exist!

Here's the code for the whole function, just to put it in context for anyone who wants to help, but the problem line is right at the end (line 48).

Update This is a really odd one: the error is caused by updating matchingTrans.Url (see penultimate line of code). Commenting out this line doesn't throw the error - even if the matchingTrans.Title still gets updated.

private static void MenuItemUpdate(int languageId, NavigationItem item)
{
    using (var db = DataContextFactory.Create<MyDataContext>())
    {
        // Select existing menu item from database.
        var dbItem =
            (from i in db.MenuItems
             where i.Id == item.Id
             select i).Single();
        // Obtain ID of link type.
        dbItem.FkLinkTypeId = GetLinkTypeByName(
            Enum.GetName(typeof (NavigationItemLinkType), item.LinkType)).Id;
        // Update the Link field with what is given.
        dbItem.Link = item.Link;
        db.SubmitChanges();

        // Item already exists and needs editing.
        // Get associated translations.
        var trans =
            from t in db.MenuItemTranslations
            where t.FkMenuItemId == item.Id
            select t;

        // If translation exists for given language, edit it.
        var matchingTrans =
            (from t in trans
             where t.FkLanguageId == languageId
             select t).SingleOrDefault();

        if (matchingTrans == null)
        {
            // No matching translation - add one.
            var newDbTrans = new MenuItemTranslation
            {
                FkMenuItemId = item.Id,
                FkLanguageId = languageId,
                Title = item.Title,
                Url = item.FriendlyUrl
            };
            db.MenuItemTranslations.InsertOnSubmit(newDbTrans);
            db.SubmitChanges();
        }
        else
        {
            // Matching translation - edit it.
            matchingTrans.Title = item.Title;
            matchingTrans.Url = item.FriendlyUrl;
            db.SubmitChanges();
            // WTF ERROR: Row not found or changed.
        }
    }
}

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

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

发布评论

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

评论(4

暮倦 2024-10-07 22:57:42

查看 SQL Profiler 的输出,它帮助我找到了这个问题的答案。生成了一段错误的 SQL,其结尾为 WHERE 0 = 1 ...这是一个明显的错误。

事实证明,该字段只是被另一位开发人员更改为允许空值,并且 Linq-to-SQL 文件没有相应更新。

简而言之,如果无缘无故生成Row not find orchange错误消息,请确保您的数据库架构与您的 .dbml 文件完全匹配,否则您将得到此错误消息出现在架构略有不同的任何字段上。

Looking at the SQL Profiler output, it helped me figure out the answer to this. There was a bad piece of SQL being generated which ended with WHERE 0 = 1 ... an obvious error.

It turns out that the field had simply been changed to allow nulls by another developer, and the Linq-to-SQL file hadn't been updated accordingly.

In short, if the Row not found or changed error message appears to be generated for no reason, make sure your database schema exactly matches your .dbml file else you'll get this error message on any fields that have slightly differing schemas.

嘦怹 2024-10-07 22:57:42

查看sql server服务器级别的连接属性“No Count”

1。在对象资源管理器中右键单击 Sql 服务器连接 --> 属性

2。转到连接选项卡/页面

3。查找默认连接选项“无计数”

4。确保未选中此选项。

Take a look at the connection property "No Count" at sql server server level

1. Right click on Sql server connection in Object Explorer -->Property

2. Go to Connection Tab/Page

3. Look for the Default connection option "no count"

4. Make sure this option is not checked.

扛刀软妹 2024-10-07 22:57:42

我发现另一种可能性可以添加到这里的优秀答案列表中:

当在数据库中使用不可为空的列时 - 然后将其映射到本质上可为空的数据类型(在本例中,数据库类型是 LONG BLOB NOT NULL 映射到 c# 中的字节数组),您最终可能会遇到使用完全相同的字节数组更新数据库会导致引发此错误的情况。

示例:您有一个网站,允许用户将图像上传到数据库。您的表有一个不可为空的 blob(sql server 中的图像,无论如何)。用户选择使用已存在的完全相同的图像来更新记录。更新检查将失败。我通过首先执行 .SequenceEqual() 检查,然后仅在传入字节数组不等于现有字节数组时才在上下文对象上调用 .SubmitChanges() 来修复此问题。

Another possibility that I've found to add to the excellent list of answers here:

When using a not-nullable column in a database - then mapping that to a datatype that is intrinsically nullable (in this example DB type is LONG BLOB NOT NULL mapped to a byte array in c#) you can end up in a situation where updating the database with the exact same byte array causes this error to be thrown.

Example: You have a website that allows the user to upload an image to the database. Your table has a blob (image in sql server, whatever) that is not nullable. The user chooses to update the record with the exact same image that is already there. The update check will fail. I fixed this by first doing a .SequenceEqual() check and then only calling .SubmitChanges() on the context object if the incoming byte array was not equal to the existing one.

九歌凝 2024-10-07 22:57:42

即使数据库模式和 dbml 完全匹配,我也遇到这个问题。问题是我试图更改实体并在单个 SubmitChanges 语句中插入实体。我通过对每个操作执行 SubmitChanges 而不是一次性执行所有操作来修复此问题。

这一切都在交易范围内,因此可能与此有关,但我不确定。

I had this issue even when the database schema and dbml matched exactly. The issue was I was trying to change an entity and insert entities in a single SubmitChanges statement. I fixed it by doing SubmitChanges on each operation instead of all at once.

This was all in a transaction scope so that may have something to do with it but I'm not sure.

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