在简单的语句没有找到或更改行LINQ C#错误 [英] Row not found or changed LINQ C# error on simple statement

查看:176
本文介绍了在简单的语句没有找到或更改行LINQ C#错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,有没有机会,这是一个多用户的问题,因为我对数据库的开发版本,在本地工作。



我我得到的不是很说明未找到行或更改当我执行db.SubmitChanges被抛出错误()。如果我打破了执行刚刚的SubmitChanges()之前发生时,我可以在SQL Server Management Studio中检查与该行的确实的存在!



下面是整个函数的代码,只要把它在上下文中的人谁愿意帮助,但问题行是正确末(48行)。



< STRONG>更新这是一个非常奇怪之一:错误是通过更新matchingTrans.Url(见代码倒数第二行)造成的。在谈到此行不会引发错误 - 即使matchingTrans.Title仍然得到更新

 私有静态无效MenuItemUpdate( INT languageId,NavigationItem项目)
{
使用(VAR DB = DataContextFactory.Create< MyDataContext>())
{
//从数据库中选择现有的菜单项。 (在db.MenuItems
从哪里i.Id == item.Id
选择我)
变种dbItem =
。单();
//获取链接类型的ID。
dbItem.FkLinkTypeId = GetLinkTypeByName(
Enum.GetName(typeof运算(NavigationItemLinkType),item.LinkType))标识。
//更新什么给出链接字段。
dbItem.Link = item.Link;
db.SubmitChanges();

//项目已经存在,需要编辑。
//获取相关的翻译。从T
无功反=
在db.MenuItemTranslations
,其中t.FkMenuItemId == item.Id
选择吨;

//如果翻译存在某种语言,编辑。
变种matchingTrans =
(从跨
t其中t.FkLanguageId == languageId
选择T).SingleOrDefault();

如果(matchingTrans == NULL)
{
//无匹配的翻译 - 添加一个。
变种newDbTrans =新MenuItemTranslation
{
FkMenuItemId = item.Id,
FkLanguageId = languageId,
标题= item.Title,
URL =项目。 FriendlyUrl
};
db.MenuItemTranslations.InsertOnSubmit(newDbTrans);
db.SubmitChanges();
}
,否则
{
//匹配的翻译 - 编辑。
matchingTrans.Title = item.Title;
matchingTrans.Url = item.FriendlyUrl;
db.SubmitChanges();
// WTF错误:未找到行或更改。
}
}
}


解决方案

纵观SQL事件探查器输出,它帮我找出这个问题的答案。有正在生成一个坏块的SQL以,其中0 = 1 ...一个明显的错误。



原来,现场只是简单地改变其他开发人员为允许空值,和LINQ到SQL文件还没有相应的更新。



在短,如果出现在未找到行或更改错误消息无故产生,确保您的数据库架构正是您的.dbml文件其他匹配你会得到关于已略有不同架构的任何字段此错误消息。


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.
        }
    }
}

解决方案

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.

这篇关于在简单的语句没有找到或更改行LINQ C#错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆