Visual Studio 2008 LINQ 删除行不起作用
我正在使用 Visual Studio 2008 和 .NET Framework 3.5。
我正在尝试做我原以为使用 LINQ to 实体简单删除一些行的事情。
我有一个简单的 SQL Server 数据库后端,我使用数据实体框架来访问它。我没有对 EDMX 中创建的默认行为进行任何操作。
我正在尝试以下代码:
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();
它到达 db.DeleteObject(OldEquip) 并生成以下错误:
“无法删除该对象,因为在 ObjectStateManager 中找不到它。”
我已经验证了这一点在执行该行代码之前,OldEquip 中有一些内容。
我添加了前两行代码(Dispose 和 new),以防以前的代码出现问题。
编辑 dbEquipmentRequestedSet 如下所示:
iRequestID (in db an int)
sCode (in db a varchar(50))
SCodePrefix (in db a varchar(10))
我使用基于 iRequestID 和 sCode 的复合主键。
编辑2 需要澄清的是,在几乎所有情况下,OldEquip 查询都会产生多个结果。
从到目前为止的答案来看,我似乎必须手动浏览每个条目才能删除。
有更优雅的解决方案吗?也许通过使用存储过程?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您想删除多个。试试这个:
可能的原因是
DeleteObject
接受一个对象,该对象应该是包含在 ObjectStateManager 中的实体。您的代码当前向其传递了一个ObjectQuery
,该对象不包含在 ObjectStateManager 中。更多信息请参见MSDN 论坛帖子。
Sounds like you want to delete multiples. Try this:
The likely cause if that
DeleteObject
accepts 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.
在本例中,
OldEquip
的类型为 IQueryable。您只能删除实际实体,因此您必须将 LINQ 语句修改为:或者如果查询返回多个值,则使用
foreach
循环遍历 IQueryable 中的所有结果并单独删除所有项目。OldEquip
in this case is of type IQueryable. You can only delete actual entities, so you will have to modify the LINQ statement to something like:or if the query returns multiple values, loop through all the results in the IQueryable using
foreach
and delete all items separately.如果您想使用它们,我有一些扩展方法可以用来一次删除多个对象:
I have some extension methods I use to delete multiple objects at once if you want to use them: