LINQ冲突 - 未找到或更改行

Tom*_*len 6 c# linq asp.net conflict

我从用户那里得到了相当频繁的错误报告,典型的一个是:

Error Message: Row not found or changed.
Stack Trace:
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) 
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) 
at Controls_Article_ArticleViewer.LoadArticle() 
at ViewTutorial.Page_Load(Object sender, EventArgs e) 
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 
at System.Web.UI.Control.LoadRecursive() 
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Run Code Online (Sandbox Code Playgroud)

(如果需要,我可以提供更多细节).该函数中唯一直接的LINQ代码LoadArticle()是:

    using (MainContext db = new MainContext())
    {
        var q = (
                    from A in db.tblArticles
                    where A.ID == this.ArticleID && A.IsDeleted == false
                    select A
                ).SingleOrDefault();
        if (q == null)
        {
            Server.Transfer("~/404.aspx");
            Context.Response.End();
        }
        else
        {
            // Cache expired for HTML generation
            if (q.HTMLLastGenerated.AddSeconds(Settings.ArticleRegenHTMLCacheSecs) < DateTime.Now)
            {
                q.Body = ArticlesCommon.Markdown(q.MarkupBody);
                q.HTMLLastGenerated = DateTime.Now;
            }

            q.Views++;
            q.LastView = DateTime.Now;
            db.SubmitChanges();

            // Set passbakcs
            this.AuthorID = q.AuthorID;
            this.Anchor = q.Anchor;
            this.SectionID = q.SectionID;
            this.Views = q.Views;
            this.DatePublished = q.Date;
            ArticleAnchor = q.Anchor;
            ArticleAuthorID = q.AuthorID;

            // Get the latest edit
            ArticleEdit LatestEdit = ArticleEditCommon.GetLatestEdit(this.ArticleID);
            // An edit exists!
            if (LatestEdit.ID != 0)
            {
                this.Description = LatestEdit.Description;
                this.ArticleTitle = LatestEdit.Title;
                ArticleBody.Text = LatestEdit.Body;
                ArticleH1.Text = ArticleTitle;
            }
            // No edits
            else
            {
                this.Description = q.Description;
                this.ArticleTitle = q.Title;
                ArticleBody.Text = q.Body;
                ArticleH1.Text = ArticleTitle;
            }

            // Get toal comments
            TotalComments = (from C in db.tblComments where C.IsDeleted == false && C.Anchor == ArticleAnchor select new { C.ID }).Count();

            // Get author details
            var qq = (from A in db.tblForumAuthors where A.Author_ID == ArticleAuthorID select new { A.Username }).Single();
            AuthorUsername = qq.Username;
        }
    }
Run Code Online (Sandbox Code Playgroud)

LoadArticle运行LINQ的引用方法中可能还有其他函数,但我猜测堆栈跟踪会以不同的方式出现,因此上面的代码就是原因.

关于什么会导致这个的任何想法?数据冲突?这种错误通常如何解决?

任何想法会导致什么?

Kir*_*oll 7

我发现L2S中的更新检查机制是一种负担而且无益.如果你需要并发保护,那么你应该把它们留进去.但在我的情况下,免除支票并让最后一个保留他的编辑对我来说已经足够了.为此,我禁用了所有列的所有更新检查,尽管您可能有理由更具辨别力.要做到这一点,使用UpdateCheck的属性ColumnAttribute定义您的属性时:

[Column(Storage="lastView", Name="LastView", 
 DbType="datetime", UpdateCheck=UpdateCheck.Never)]
public DateTime LastView { ... }
Run Code Online (Sandbox Code Playgroud)

话虽这么说,我不相信sqlmetal/dbml中有任何设施可以为你做这个,所以你必须自己生成实体类(无论如何都不是一个坏主意 - 非常强大).

  • 我认为问题是Linq To Sql会在更新时检查您的列,有时可能会在.dbml文件中更改SQL Server中的列.以下是我发现有用的替代答案:http://stackoverflow.com/questions/4104797/row-not-found-or-changed-linq-c-sharp-error-on-simple-statement-help-please (2认同)