Fernando
Fernando

Reputation: 1249

Entity Framework adding / deleting a new object

Suppose I add a new object to an EntityCollection:

myThingHolder.Things.Add(myThing);

... then later, using the same ObjectContext, before ever saving to the database, I do:

myObjectContext.Things.DeleteObject(myThing);

I get an exception: "The object cannot be deleted because it was not found in the ObjectStateManager."

Other than doing

myThingHolder.Things.Remove(myThing);

is there another solution? I'd like to be able to independently delete the object--just like I can do if the object has been saved previously.

EDIT

I should note that this problem only occurs when myThingHolder is also new and has not yet been saved to the database.

Upvotes: 1

Views: 185

Answers (1)

Kyle
Kyle

Reputation: 3300

I think you need to attach the object to your context. Take a look at attach on MSDN for more information.

Upvotes: 1

Related Questions