0

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.

Community
  • 1
  • 1
vivid
  • 1,115
  • 2
  • 14
  • 34

2 Answers2

2

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.

Community
  • 1
  • 1
Paul Griffin
  • 2,416
  • 15
  • 19
  • But when it is other way just add it change the quote.ID + 1 then saving as new object any idea why? – vivid Mar 05 '15 at 19:43
  • 1
    I think your code may be muddying things up a bit for you, `quote` could be one of two things, either an entity fetched from the database, in which case EF should track its changes automatically and update the existing row. OR if that's not found, `quote` is a new object created in memory, in which case no matter how you edit it, EF will save it as a new row in the database. Try doing just a simple fetch-edit-save and see if that behaves the way you expect. – Paul Griffin Mar 05 '15 at 19:53
  • 1
    So just to clarify, you are: – Paul Griffin Mar 05 '15 at 21:00
  • 1
    If `quote` is created in memory, which your code makes difficult to tell because of the `??` operator, it doesn't matter how many changes you make to `quote`'s properties, `quote` will be inserted as a new row when you call `SaveChanges()`. If the query for assigning `quote`!= null, then EF should track any changes you make to `quote` and update the existing row when you call `SaveChanges()`. If this is not what is happening, Try doing a very simple interaction like `var testObj = ctx.HB_Quote.First(x => x.ID == 6); testObj.ID = 8; ctx.SaveChanges();` and verify that that works as expected. – Paul Griffin Mar 05 '15 at 21:14
1

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). So, is any field in the HB_ImageLibrary table marked as TimeStamp, of Fixed?

What is the relation between the database and the entities? Model first, Db first, code first?

Dabblernl
  • 15,831
  • 18
  • 96
  • 148