使用.SaveChanges()保存通过引用传递的EF对象



问题:

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

已尝试:

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

可能的问题:

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

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

这里有一些代码:

公用子撤消重做操作(IdNow为整数(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 是我试图修复的,但现在它给出了一个错误。非常感谢您的帮助!谢谢

好吧,多亏了@krillgar,我发现了问题所在。我声明了多个上下文,并试图保存到错误的上下文中。所以,解决方案是将我的函数合并为一个函数,只是为了确保我在一个上下文中做所有事情。因此,现在我的函数声明了一个新的上下文,从其相应的数据表中获取对象,执行所有检查,设置回值,并将所有内容保存在一个函数中。就这么简单!

最新更新