noobiehacker
noobiehacker

Reputation: 1109

database context insert and delete bug

I am trying to insert some entity into the databse context and delete it.

The trouble I have right now is when I try to delete it , it gives me this error.

System.InvalidOperationException : The object cannot be deleted because it was not found in the ObjectStateManager.

So I googled and people tell me to call .attach() to the entity, after I did that I got

System.InvalidOperationException : An object with the same key already exists in the       
ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same   
key.

So either way , its not working - -

Some Code:

    public void delete(WebPointOrderHead item)
    {
        WebDataEntities3 dbcDelete = this.dbc;
        WebPointOrderHead deleteItem = dbcDelete.WebPointOrderHeads.Where(p => (p.OrderID == item.OrderID)).First();
        dbcDelete.WebPointOrderHeads.Remove(item) ;
        dbcDelete.SaveChanges();
    }

    public void insert(WebPointOrderHead item)
    {

        WebDataEntities3 dbcinsert = this.dbc;
        WebPointOrderHead itemToAdd = BusinessLogic.calculatePoints(item, dbcinsert);
        var IDs = from id in dbcinsert.WEBIDs
                  where id.TableName.Equals("PointOrder_ID")
                  select id;
        IDs.First().NextID++;
        var highestID = (from c in dbcinsert.WebPointOrderHeads 
                         select c.OrderID).Max();
        itemToAdd.OrderID = highestID + 1;
        dbcinsert.WebPointOrderHeads.Add(itemToAdd);
        dbcinsert.SaveChanges();
    }

Upvotes: 0

Views: 245

Answers (1)

Justin Donohoo
Justin Donohoo

Reputation: 380

   public void delete(WebPointOrderHead item)
{
    WebDataEntities3 dbcDelete = this.dbc;
    WebPointOrderHead deleteItem = dbcDelete.WebPointOrderHeads.Where(p => (p.OrderID == item.OrderID)).First();
    dbcDelete.WebPointOrderHeads.Remove(item) ;
    dbcDelete.SaveChanges();
}

A few things here about this snippet.

1) You can clean up your LINQ to be like this:

    WebPointOrderHead deleteItem = dbcDelete.WebPointOrderHeads.First(x=>x.OrderID == item.OrderID); 

My prefered method of choice would look more like this:

WebPointOrderHead deleteItem = dbcDelete.WebPointOrderHeads.FirstOrDefault(x=>x.OrderID == item.OrderID);
if(deleteItem == null)return;

This way you have a little cleaner LINQ and only try to remove the record if it was found in the context.

2) You are querying the context for the entity, but then trying to remove the detached entity. You need to change .Remove(item) to .Remove(deleteItem); The context has no idea what item is.

3) Addressing your Insert/Update logic. When you are working with detatched entities, you need to check the ID. If the ID is > 0 then you need to use .Attach(), if the ID is 0 you need to use .Add()

4) I'm confused by your insert block of code, it looks to me like you are trying to control the IDs of your entities yourself. This has many issues: Not Thread Safe, Not Needed, Bad, etc... If you Add() an entity to a context that does not have an ID it will be inserted, and your identity will be assigned assuming you have an Identity column. If you Attach() an entity with an existing ID, then EF will generate an Update statement.

Upvotes: 2

Related Questions