Visual Studio 2008 LINQ 删除行不起作用

发布于 2024-11-07 20:37:31 字数 1171 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一直在等你来 2024-11-14 20:37:31

听起来您想删除多个。试试这个:

var OldEquip = (from u in myDB.dbEquipmentRequestedSet
                            where u.iRequestID == requestID
                            select u)
                    .ToList();

foreach(var item in OldEquip)
{
    myDB.DeleteObject(item);
}

可能的原因是 DeleteObject 接受一个对象,该对象应该是包含在 ObjectStateManager 中的实体。您的代码当前向其传递了一个 ObjectQuery ,该对象不包含在 ObjectStateManager 中。

更多信息请参见MSDN 论坛帖子

Sounds like you want to delete multiples. Try this:

var OldEquip = (from u in myDB.dbEquipmentRequestedSet
                            where u.iRequestID == requestID
                            select u)
                    .ToList();

foreach(var item in OldEquip)
{
    myDB.DeleteObject(item);
}

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 an ObjectQuery<T> which isn't contained in the ObjectStateManager.

More at this MSDN forum post.

梦一生花开无言 2024-11-14 20:37:31

在本例中,OldEquip 的类型为 IQueryable。您只能删除实际实体,因此您必须将 LINQ 语句修改为:

var OldEquip = (from u in myDB.dbEquipmentRequestedSet
                where u.iRequestID == requestID
                select u).First();

或者如果查询返回多个值,则使用 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:

var OldEquip = (from u in myDB.dbEquipmentRequestedSet
                where u.iRequestID == requestID
                select u).First();

or if the query returns multiple values, loop through all the results in the IQueryable using foreach and delete all items separately.

狼性发作 2024-11-14 20:37:31

如果您想使用它们,我有一些扩展方法可以用来一次删除多个对象:

    public static void DeleteAll(this ObjectContext context, IEnumerable<object> collection)
    {
        foreach (var item in collection.ToList())
        {
            context.DeleteObject(item);
        }
    }

    public static void DeleteAll<TEntity>(this ObjectSet<TEntity> context, IEnumerable<TEntity> collection) where TEntity : class
    {
        foreach (var item in collection.ToList())
        {
            context.DeleteObject(item);
        }
    }

I have some extension methods I use to delete multiple objects at once if you want to use them:

    public static void DeleteAll(this ObjectContext context, IEnumerable<object> collection)
    {
        foreach (var item in collection.ToList())
        {
            context.DeleteObject(item);
        }
    }

    public static void DeleteAll<TEntity>(this ObjectSet<TEntity> context, IEnumerable<TEntity> collection) where TEntity : class
    {
        foreach (var item in collection.ToList())
        {
            context.DeleteObject(item);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文