代码改变世界

Cannot remove an entity that has not been attached

2009-07-15 11:40  蜡笔小旧  阅读(877)  评论(0编辑  收藏  举报

[TestMethod()]
//[ExpectedException(typeof(System.ArgumentNullException))]
public void DeleteTest()
{
    Ervice target = new Ervice (con);
    Guid erviceID = new Guid("{C49ACB94-F51F-45FF-8D1B-5FBBC9BC3C00}");
    target.Delete(erviceID );
}

异常:

Test method TestProjectCategoryEnum.EmbankmentAreaServiceTest.DeleteTest threw exception:  System.InvalidOperationException: Cannot remove an entity that has not been attached..

MSDN:

可对一些实体使用 Attach 方法。这些实体在一个 DataContext 中创建并序列化到客户端,随后被反序列化回来,以便执行更新或删除操作。

Table<(Of <(TEntity>)>)..::.Attach 方法

将实体附加到 DataContext

 

var l = this.ReadonlyDataContext.Es.Single(p => p.EmbankmentID == embankmentID);
this.Es.EmbankmentAreas.Attach(l);
this.Es.EmbankmentAreas.DeleteOnSubmit(l);
this.Es.SubmitChanges();