如何使用Linq To实体删除行 [英] How Do I Delete A Row With Linq To Entities

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

问题描述

您好我在这里发布我的代码

Hi I post here my code

Public Shared Function delete_config(ByVal configurazione As CanoniConfig) As Boolean
       Dim result As Boolean = False
       Dim configurazione_to_delete As ConfigurazioniCanoniFissi = Nothing

       Try
           Using dbContext As New P2000ProdwareEntities
               configurazione_to_delete = getFirstConfig(configurazione)
               If Not configurazione_to_delete Is Nothing Then
                   dbContext.DeleteObject(configurazione_to_delete)
                   dbContext.SaveChanges()
               End If
               result = True
           End Using
       Catch ex As Exception
           result = False
       End Try
       Return result
   End Function



configurazione_to_delete是需要的行的实例删除,这不是什么。



如果我运行此代码,我会得到这个

无法删除该对象,因为它在ObjectStateManager中找不到。



有什么问题?

谢谢!


configurazione_to_delete is the instance to the row that need to be deleted and it's not nothing.

If I run this code I obtain this
"Can not delete the object because it was not found in ObjectStateManager."

What is wrong?
Thank you!

推荐答案

完成。

问题是在使用语句之间更新或删除行的操作必须是

l所以如果你得到getfirst配置的行你的修改没有效果。

所以代码是这个







Done.
The problem was that the operation of updating or deleting rows must be
l between using statement so if you get the row with getfirst config your modifying that have no effect.
so the code is this



Public Shared Function delete_config(ByVal configurazione As CanoniConfig) As Boolean
    Dim result As Boolean = False
    Dim configurazione_to_delete As ConfigurazioniCanoniFissi = Nothing
    Try
        Using dbContext As New P2000ProdwareEntities
            configurazione_to_delete = (From e In dbContext.ConfigurazioniCanoniFissis
                                  Where e.ChFase = configurazione.chfase And e.ChTecnica = configurazione.chtecnica And e.Commessa = configurazione.commessa And e.ChCliente = configurazione.chcliente
                                  Select e).First
            If Not configurazione_to_delete Is Nothing Then
               dbContext.DeleteObject(configurazione_to_delete)
                dbContext.SaveChanges()
            End If
            result = True
        End Using
    Catch ex As Exception
        result = False
    End Try
    Return result
End Function


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

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