I am working on Entity Framework 4.0 . Here Adding control into database using AddObject() and save that suing SaveChange() methods.
But once I delete that added control and try to add again same I am getting this error again and again
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh
ObjectStateManager
entries.
I am not able to add it. Once I close my application then try to add then I am able to add that control.
I tried to search a lot here and there how it going wrong but could not find solution. As I am new born in field in Entity Framework.
public void Add(SearchPath entity) {
_context.SearchPaths.AddObject(entity);
// _context.Save(entity, false);
}
public void Remove(SearchPath entity)
{
if (entity.Path != null)
{
using (EntityContext entityObj = new EntityContext())
{
entityObj.SearchPaths.Attach(entity);
entityObj.SearchPaths.DeleteObject(entity);
entityObj.SaveChanges();
}
}
}
public void Modify(SearchPath entity)
{
using (EntityContext entityObj = new EntityContext())
{
try
{
entityObj.SearchPaths.Attach(entity);
entityObj.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
entityObj.SaveChanges();
}
catch (OptimisticConcurrencyException)
{
entityObj.Refresh(RefreshMode.StoreWins, entity);
entityObj.SaveChanges();
}
}
}
public void Add(Package entity)
{
_context.Packages.AddObject(entity);
}
public void Remove(Package entity)
{
if (_context.GetEntityState(entity) == EntityState.Unchanged)
_context.Packages.Attach(entity);
_context.Packages.DeleteObject(entity);
}