EF optimistic concurrency exceptions need to fix

c# database entity-framework orm

Question

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

Additional information: Store update, insert, or delete statement affected an unexpected number of rows (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.

1
0
3/5/2015 6:40:32 PM

Popular Answer

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):

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. Now you're telling EF explicitly that it has been modified, which means that it will try to update it when you call 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.

EF is good at tracking changes for you, you rarely need this level of state management (See here for a little bit more info). Removing these code blocks should fix your problem.

2
5/23/2017 11:45:43 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow