Store update, insert, or delete statement affected an unexpected . Entities modified or deleted since entities were loaded
Solution 1
The problem is here:
OBJgroupRepository.Edit(gr);
OBJgroupRepository.Save();
You called the OBJgroupRepository
twice! This causes race condition (and subsequently concurrency consideration) between threads. Try to use save method contents inside edit.
Solution 2
I know this is late, but this happened to me today and I couldn't figure out why. I am using code first and had to make a major change in a primary key. when I did i started getting this same error. I looked into it for about 2 hours and finally figured out that I was trying to insert data into a primary key. I thought that I would let people know this could be a reason that you can get this error.
Ehsan Akbar
Since January 2009, have 6 years plus of extensive hands on experience of website development. An experienced team lead and team player with excellent communication and interpersonal skills who has the ability to work independently under pressure Currently working as Senior Software Engineer/Team Lead at Ministry Of Interior. Masters in 2016 from the Azad University College of Information Technology, Qazvin, Iran. Leader and Co-Founder of software company SPAD System in 2013, Tehran, Iran
Updated on June 06, 2022Comments
-
Ehsan Akbar almost 2 years
I am using MVC for my project .
I added a controller named
group
and in this controller i have some action as usual like create and edit and etc .but my problem refers to
edit
method As you can see here :public ActionResult Edit(int GroupId) { ViewBag.groupIdLST = new SelectList(OBJgroupRepository.FindBy(i => i.GroupId == null).ToList(), "Id", ViewBag.GroupType = new SelectList(OBJgroupRepository.FindBy(i => i.GroupId == null).ToList(), "name", DomainClass.Group tg = OBJgroupRepository.FindBy(i => i.Id == GroupId).First(); return View(tg); } [HttpPost] public ActionResult Edit(Group gr) { if (ModelState.IsValid) { OBJgroupRepository.Edit(gr); OBJgroupRepository.Save(); } TempData["success"] = "اطلاعات با موفقیت ویرایش شد"; return RedirectToAction("Create"); }
When I click on edit button i got this error :
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.
My edit and save method :
public virtual void Edit(T entity) { _entities.Entry(entity).State = System.Data.Entity.EntityState.Modified; } public virtual void Save() { try { _entities.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
my repository :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DomainClass; using ProjectModel; namespace Repository { public class GroupRepository : GenericRepository<DataAccessModelContainer, Group> { } }
Any details are available as requested.
Best regards .