The following error occurs during the Ling operation:
Cannot remove an entity that has not been attached
My error is that different data context is used for query and deletion in the same method.
ErrorCodeAs follows:
Protected Dataclassesdatacontext DB
{
Get
{
Return New Dataclassesdatacontext ();
}
}
Private Void Deleteogu ( Int ID)
{
VaR OGU = DB. Ogus. firstordefault (x => X. ogu_sysid = ID );
DB. Ogus. deleteonsubmit (OGU );
DB. submitchanges ();
Binddata ();
}
In the delete method, DB is a new instance every time. Therefore, the parameter OGU of deleteonsubmit is not in the same object as the data context of the method, and an error is displayed as shown in the title.
Remove the DB attribute and move it to the delete method.
Private Void Deleteogu ( Int ID)
{
VaR DB = New Dataclassesdatacontext ();
VaR OGU = DB. Ogus. firstordefault (x => X. ogu_sysid = ID );
DB. Ogus. deleteonsubmit (OGU );
DB. submitchanges ();
Binddata ();
}
In this case, all the databases in the method point to the same object, and errors are excluded.
From: http://blog.csdn.net/duwa789/archive/2010/05/06/5563714.aspx