I am using Visual Studio 2008 with .NET Framework 3.5.
I am trying to do what I had thought would be a simple deletion of some rows using LINQ to entities.
I have a simple SQL Server database back end and I am using Data Entity Framework to access it. I have done nothing to the default behavior created in the EDMX.
I am attempting the following code:
myDB.Dispose();
myDB = new EChODatabaseConnection();
//get our equipment list of requested equipment
var OldEquip = from u in myDB.dbEquipmentRequestedSet
where u.iRequestID == requestID
select u;
myDB.DeleteObject(OldEquip);
myDB.SaveChanges();
It gets to the db.DeleteObject(OldEquip) and generates the following error:
“The object cannot be deleted because it was not found in the ObjectStateManager.”
I have verified that there is something in OldEquip before the line of code is executed.
I added the first two lines of code(Dispose and new) on the off chance there was an issue with previous code.
EDIT
The dbEquipmentRequestedSet looks like this:
iRequestID (in db an int)
sCode (in db a varchar(50))
SCodePrefix (in db a varchar(10))
I use a composite primary key based upon iRequestID and sCode.
EDIT2
To clarify, in almost all cases, the OldEquip query will result in more than one result.
From the answers so far, it appears that I will have to manually go through each entry to delete.
Is there a more elegant solution? Maybe by using a stored procedure?
Sounds like you want to delete multiples. Try this:
The likely cause if that
DeleteObjectaccepts an object, which should be an entity that is contained in the ObjectStateManager.Your code currently passed it anObjectQuery<T>which isn’t contained in the ObjectStateManager.More at this MSDN forum post.