我有一个方法,它接收IEnumerable<Guid>我想要删除的对象的ID.一种建议的方法如下
foreach(Guid id in ids)
{
var tempInstance = new MyEntity { Id = id };
DataContext.Attach(tempInstance); // Exception here
DataContext.Remove(tempInstance);
}
Run Code Online (Sandbox Code Playgroud)
如果对象尚未加载到内存中,这可以正常工作.但我的问题是,当它们已经加载时,该Attach方法抛出一个InvalidOperationException - The instance of entity type 'MyEntity' cannot be tracked because another instance with the key value 'Id:...' is already being tracked.如果我DataContext.Remove不使用电话就会发生同样的事情Attach.
foreach(Guid id in ids)
{
var tempInstance = new MyEntity { Id = id };
DataContext.Remove(tempInstance); // Exception here
}
Run Code Online (Sandbox Code Playgroud)
我不想用来DataContext.Find抓取已经加载的对象的实例,因为如果它尚未加载,它会将对象加载到内存中.
我不能DataContext.ChangeTracker …