简体   繁体   English

EF乐观并发异常需要修复

[英]EF optimistic concurrency exceptions need to fix

Hello guys so i've tryed some ideas over internet to fix this issue and all have faild so that why i'm writing this so maybe someone can help me in entity framework latest version:) 大家好,我尝试了一些想法来解决此问题,但都失败了,所以我为什么要写这篇文章,以便有人可以在最新的实体框架中为我提供帮助:)

 using (var ctx = new ESContext())
            {
                quote =
                      ctx.HB_Quote.FirstOrDefault(x => x.ID == issuesContract.EvidenceContract.QuoteContract.ServerID) ??
                      new ESModel.HB_Quote()
                      {
                          ID = issuesContract.EvidenceContract.QuoteContract.ServerID ?? 0,
                          QuoteLegend = issuesContract.EvidenceContract.QuoteContract.QuoteLegend,
                          QuoteText = issuesContract.EvidenceContract.QuoteContract.QuoteText
                      };
                if (issuesContract.EvidenceContract.QuoteContract.ServerID == null)
                {
                    ctx.HB_Quote.Add(quote);
                }
                else
                {
                    ctx.Entry(quote).State = EntityState.Modified;
                }
                ctx.SaveChanges();
            }
            using (var ctx = new ESContext())
            {
                imageLibrary =
                       ctx.HB_ImageLibrary.FirstOrDefault(
                           x => x.ID == issuesContract.EvidenceContract.ImageLibaryContract.ServerID) ??
                       new ESModel.HB_ImageLibrary()
                       {
                           ID = issuesContract.EvidenceContract.ImageLibaryContract.ServerID ?? 0,
                           Conclusion = issuesContract.EvidenceContract.ImageLibaryContract.Conclusion,
                           Image = issuesContract.EvidenceContract.ImageLibaryContract.Image,
                           Title = issuesContract.EvidenceContract.ImageLibaryContract.Title
                       };
                if (issuesContract.EvidenceContract.ImageLibaryContract.ServerID == null)
                {
                    ctx.HB_ImageLibrary.Add(imageLibrary);
                }
                else
                {
                    ctx.Entry(imageLibrary).State = EntityState.Modified;
                }
                ctx.SaveChanges();
            }

the last part co using this error: 最后一部分使用此错误:

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型为'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException'的异常,但未在用户代码中处理

Additional information: Store update, insert, or delete statement affected an unexpected number of rows (0). 附加信息:存储更新,插入或删除语句影响了意外的行数(0)。 Entities may have been modified or deleted since entities were loaded. 自加载实体以来,实体可能已被修改或删除。 See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions. 有关了解和处理乐观并发异常的信息,请参见http://go.microsoft.com/fwlink/?LinkId=472540

At first glance, I believe that this code is causing your problem in the first using block (as well as the similar code in the second block): 乍一看,我相信这段代码会在第一个using块中引起您的问题(以及第二个块中的类似代码):

else
{
    ctx.Entry(quote).State = EntityState.Modified;
}

At this point in your code, you've retrieved a valid record from EF, but you haven't done anything with it. 至此,在代码中,您已经从EF中检索了有效记录,但尚未对其进行任何处理。 Now you're telling EF explicitly that it has been modified, which means that it will try to update it when you call SaveChanges() . 现在,您要明确地告诉EF它已被修改,这意味着当您调用SaveChanges()时它将尝试更新它。 It expects to get a count from the database of rows updated == 1, but instead no rows have actually been updated, so it gets a count of 0, causing your error. 它期望从数据库中获得更新的行== 1的计数,但是实际上没有行被更新,因此它的计数为0,从而导致错误。

EF is good at tracking changes for you, you rarely need this level of state management ( See here for a little bit more info ). EF擅长为您跟踪更改,您几乎不需要这种状态管理级别( 有关更多信息,请参见此处 )。 Removing these code blocks should fix your problem. 删除这些代码块应该可以解决您的问题。

Paul's analysis (zero rows were updated where one was expected) seems correct to me. 保罗的分析(零行被更新,原本应该是零行)对我来说似乎是正确的。 Usually the cause of this error is a so called TimeStamp field that is used to catch concurrency problems (a situation in which two users edit the same row independently from each other, but at the same time). 通常,导致此错误的原因是所谓的TimeStamp字段,用于捕获并发问题(两种用户彼此独立但同时编辑同一行的情况)。 So, is any field in the HB_ImageLibrary table marked as TimeStamp, of Fixed? 那么,HB_ImageLibrary表中的任何字段是否标记为“固定”的“时间戳记”?

What is the relation between the database and the entities? 数据库和实体之间是什么关系? Model first, Db first, code first? 首先是模型,首先是Db,然后是代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM