Reputation: 1145
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.
Upvotes: 0
Views: 7645
Reputation: 16141
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?
Upvotes: 1
Reputation: 2436
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.
Upvotes: 2