LINQ实体删除实体 [英] LINQ to Entities; Deleting an entity

查看:104
本文介绍了LINQ实体删除实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的程序遇到了一个非常大的问题.

基本上,我的程序应该做的是使用LINQ通过ID来查询正确的实体,然后将其删除.

事实是,它永远不会删除正确的实体,除非它是列表中的最后一个实体.它总是删除紧随其后的实体.

这是我在整个程序中一直使用的代码.

I''ve encountered a really big problem in a program I''m working on.

Basically, what my program is suppose to do, is use LINQ to query the correct entity by its ID and then delete it.

The thing is, it won''t ever delete the correct entity unless its the last entity in the list. It always deletes the entity right after it.

Here''s the code I''ve been using throughout the program.

Dim Cat = From c In PE.Categories
                  Where c.CategoryID = lblCategoryID.Text
                  Select c
        Try
            For Each c As NEIPurchasingDAL.Category In Cat
                PE.DeleteObject(c)
            Next
        Catch ex As Exception
            MessageBox.Show("There are no more categories to delete.", _
                     "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

PE.SaveChanges()



我已经尝试了一些代码工作,但是没有运气,而且我似乎无法在网上找到任何东西.

更新:
我提出的解决方案并没有像我最初想到的那样解决问题.我仍然遇到同样的问题,即它并不总是删除正确的实体,尽管我提出的解决方案似乎更准确,但并不完美.



I''ve tried working around with my code a little and had no luck, and I can''t seem to find anything online.

Update:
The solution I proposed didn''t fix the problem like I first thought. I still have the same problem of it not always deleting the correct entity, the solution I proposed seems to be more accurate though, but not perfect.

推荐答案

有了一点点,我找到了解决自己问题的方法.

我所做的是将一个变量设置为一个对象,该对象等于绑定源的当前值.然后,我暂停了绑定源,并调用了Context.DeleteObject()方法,最后恢复了绑定.

这解决了我的问题.

这是一个例子,以防其他人遇到同样的问题.

After messing around with it a little bit, I found a solution to my own problem.

What I did is set a variable as an object equal to the current value of the binding source. Then I suspended the binding source and called the Context.DeleteObject() method and finally resumed binding.

This fixed my problem.

Here''s an example in case anyone else runs into this same problem.

Dim (variable) As (object) = (Name)BindingSource.Current
(Name)BindingSource.SuspendBinding()
(Context).DeleteObject((variable))
(Name)BindingSource.ResumeBinding()



这是我使用的实际代码.



And here''s the actual code I used.

Dim CurrentObject As NEIPurchasingDAL.Category = CategoryBindingSource.Current
CategoryBindingSource.SuspendBinding()
PE.DeleteObject(CurrentObject)
CategoryBindingSource.ResumeBinding()



更新:
我提出的解决方案并没有像我最初想到的那样解决问题.我仍然遇到同样的问题,即它并不总是删除正确的实体,尽管我提出的解决方案似乎更准确,但并不完美.



Update:
The solution I proposed didn''t fix the problem like I first thought. I still have the same problem of it not always deleting the correct entity, the solution I proposed seems to be more accurate though, but not perfect.


这篇关于LINQ实体删除实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆