gpt4 book ai didi

c# - LINQ 冲突 - 行未找到或未更改

转载 作者:太空狗 更新时间:2023-10-29 21:19:25 25 4
gpt4 key购买 nike

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

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)

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

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

LoadArticle 中可能还有其他函数引用了运行 LINQ 的方法,但我猜测堆栈跟踪的结果会有所不同,所以上面的代码是原因。

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

有什么想法会导致这种情况吗?

最佳答案

我发现 L2S 中的更新检查机制是一种负担且毫无帮助。如果您需要并发保护,那么您可能应该将它们留在里面。但就我而言,免除检查并让最后一个进来的人保留他的编辑对我来说已经足够了。为此,我禁用了所有专栏的所有更新检查,尽管您可能有理由更加挑剔。为此,请在定义属性时使用 ColumnAttributeUpdateCheck 属性:

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

话虽如此,我认为 sqlmetal/dbml 中没有任何工具可以为您执行此操作,因此您必须自己生成实体类(无论如何这不是一个坏主意 - 非常强大)。

关于c# - LINQ 冲突 - 行未找到或未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6565612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com