使用.SaveChanges()

时间:2016-08-18 14:43:45

标签: vb.net entity-framework

问题:

我可以在从数据库中提取对象后对其进行更改,但是我无法通过Context.SaveChanges()方法保存对象。

已经尝试过:

向对象添加修改后的跟踪器(给出错误),阅读有关使用SaveChanges方法的文章,在进行更改后立即保存。

可能出现的问题:

对象正由ref传递给多个函数。

我通常对EF没有问题,但我在这里使用了很多反思。我正在为一个需要回顾对象的相当大的程序添加一个撤销按钮。单击撤消时,将拉动实际对象,针对“后对象”进行测试,如果它们匹配,则函数会计算出更改的属性值,并设置before对象的值。然后保存更改并更新数据库。

以下是一些代码:

Public Sub UndoRedoAction(IdNow As Integer)         Dim Db As New Luk_StackUp_ProgramEntities

    'First we need to find the object in the list given the id in UndoRedoLog
    Dim ObjectNow As ClsUndoRedoItem

    Try

        ObjectNow = (From a In UndoRedoLog Where a.ID = IdNow).SingleOrDefault

    Catch ex As Exception   'Fail

        ObjectNow = Nothing
        MsgBox("Could not find the item. Either does not exist or there is multiple with same id.  " + ex.ToString)

        Exit Sub
    End Try

    'Is this a new object?
    If ObjectNow.ID = -1 Then

        MsgBox("This object does not exist yet")

    End If

    'See if after object is the same as the object in the database
    Dim ActualObject As Object = ""

    'if this is a redo object, don't check since all of that has already been done
    If ObjectNow.WhatGroupChanged.isRedo = True Then

        GetDataBaseObject(ObjectNow.AfterObject, ActualObject)

    Else

        Try 'Pass the after object since that is the object that should be stored in Db
            Call TestActualObject(ObjectNow.AfterObject, ActualObject)

        Catch ex As Exception   'Fail
            MsgBox(ex.ToString)
        End Try

    End If

    'Now set the actaul object to before if it is not null
    If Not (ActualObject Is Nothing) Then

        Try 'Set to the before object

            'Need to find change and set that value
            SetChangedProperties(ActualObject, ObjectNow.BeforeObject)
            Db.Entry(ActualObject).State = Entity.EntityState.Modified

        Catch ex As Exception
            'Failed

            MsgBox(ex.ToString)
            Exit Sub
        End Try

    ElseIf ObjectNow.WhatGroupChanged.isRedo Then   'For redo objects only

        'Dont allow, does not match what is in the database
        MsgBox("Cannot Redo object")

        'Remove from list
        UndoRedoLog.Remove(ObjectNow)

        Exit Sub
    Else

        'Dont allow, does not match what is in the database
        MsgBox("Cannot Undo object")

        'Remove from list
        UndoRedoLog.Remove(ObjectNow)
        Exit Sub

    End If

    'Switch list
    ObjectNow.WhatGroupChanged.isRedo = Not (ObjectNow.WhatGroupChanged.isRedo)

    'switch the before and after objects
    Dim TempObject As Object = CopyObject(ObjectNow.BeforeObject)
    ObjectNow.BeforeObject = CopyObject(ObjectNow.AfterObject)
    ObjectNow.AfterObject = CopyObject(TempObject)

    'Set the id higher up on the list
    GlobalCounter.ObjectCounter += 1
    ObjectNow.ID = GlobalCounter.ObjectCounter

    'Now save changes
    Db.SaveChanges()

此代码:Db.Entry(ActualObject).State = Entity.EntityState.Modified 是我尝试修复的内容,但现在它给出了错误。任何帮助是极大的赞赏!谢谢!

1 个答案:

答案 0 :(得分:0)

好的,谢谢@krillgar我已经弄清楚出了什么问题。我正在声明多个上下文并试图保存到错误的上下文。因此,解决方案是将我的功能合并为一个,以确保我在一个上下文中完成所有操作。所以现在我的函数声明了一个新的上下文,从各自的数据表中获取对象,完成所有检查,将值设置回来,并将所有内容保存在一个函数中。就这么简单!