1

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);
} 
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Prem Prakash
  • 203
  • 2
  • 12
  • 1
    Your context object instance is created and disposed for every request, or cached and shared through requests? – Teddy Dec 05 '12 at 06:09
  • I think it's same as post below. http://stackoverflow.com/questions/1836173/entity-framework-store-update-insert-or-delete-statement-affected-an-unexpec – Teddy Dec 05 '12 at 06:12
  • I am not getting ... But in this problem i have two entity items to handle. Once is path and other is some package. While adding path and editing it and saving it. I was getting same error. So i created new context and attached it while removing and called savechange. It worked fine. But when i doing same for other entity like package i am getting error while saving it. It doesnt give error first time but it does second time only while Savechange method call. Need Some idea to deal It. It is going hey way over my head. – Prem Prakash Dec 05 '12 at 06:32
  • Just show your code here, to include how you get the context, and how you add, delete object within this context – Teddy Dec 05 '12 at 07:06
  • 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(); } } } – Prem Prakash Dec 05 '12 at 08:22
  • 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(); } } } – Prem Prakash Dec 05 '12 at 08:23
  • public void Add(Package entity) { _context.Packages.AddObject(entity); – Prem Prakash Dec 05 '12 at 08:24
  • public void Remove(Package entity) { if (_context.GetEntityState(entity) == EntityState.Unchanged) _context.Packages.Attach(entity); _context.Packages.DeleteObject(entity);} – Prem Prakash Dec 05 '12 at 08:24
  • public void Save(object entity, bool IsDelete) {using (var transaction = Connection.BeginTransaction()) { try { SaveChanges(); transaction.Commit(); } catch (OptimisticConcurrencyException) {if(IsDelete == true) { Refresh(RefreshMode.ClientWins, entity); SaveChanges(); transaction.Commit(); } else {SaveChanges();} } } – Prem Prakash Dec 05 '12 at 08:28
  • After every add remove and modify i used to calls save method. For both package and searchpath . Save method is last added comment. – Prem Prakash Dec 05 '12 at 08:29

2 Answers2

1

Answer for above problem is Just call your own save method like this.

public void Save(object entity)
{
    using (var transaction = Connection.BeginTransaction())
    {

        try
        {
            SaveChanges();
            transaction.Commit();
         }
         catch (OptimisticConcurrencyException)
         {
             if (ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Deleted || ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Modified)
                    this.Refresh(RefreshMode.StoreWins, entity);
              else if (ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Added)
                    Detach(entity);
              AcceptAllChanges(); 
              transaction.Commit();
          }

    }
}
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Prem Prakash
  • 203
  • 2
  • 12
0

once I delete that added control and try to add again

The problem is that you apparently do that in the same context. To me that indicates that the context's lifespan is too long. The best way to use context instances in EF is one context per unit of work (or business transaction, or use case).

So if you want to delete a control: create a context, delete the the control, dispose the context. If you want to add it again (for some reason): create a context, add the control, dispose the context. From your code snippets it is not entirely clear to me if this happens in your code.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291