2016-08-18 1 views
0

이 문제 저장 :.SaveChanges에 참조로 전달 된 EF 객체()

내가 변화 데이터베이스로부터 뽑아 후에 이의를 만들 수 howerver, I는 Context.SaveChanges 통해 객체() 메소드를 저장할 수있다.

이 이미 시도 :

는, 객체에 대한 수정 추적기 (오류를 제공) 추가하기 SaveChanges를 방법을 사용하는 방법에 대한 기사를 읽고, 저장은 변경 직후.

가능한 문제 :

객체는 여러 가지 기능에 심판에 의해 전달되고있다.

저는 일반적으로 EF에 문제가 없지만 여기서 많은 반향을 사용하고 있습니다. 개체를 다시 살펴 봐야하는 다소 큰 프로그램에 실행 취소 단추를 추가하려고합니다. 실행 취소를 클릭하면 실제 객체가 꺼내 짐에 따라 '객체 후'에 대해 테스트되고 일치하면 어떤 속성 값이 변경되었는지를 파악하고 before 객체의 값을 설정합니다. 그런 다음 변경 사항을 저장하고 데이터베이스를 업데이트해야합니다.

'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() 

이 코드

공공 하위 UndoRedoAction (정수로 IdNow) 새로운 Luk_StackUp_ProgramEntities으로 희미한 DB : Db.Entry(ActualObject).State = Entity.EntityState.Modified 내가 해결하기 위해 뭘하려하지만, 지금 여기

몇 가지 코드 오류가 발생합니다. 어떤 도움이라도 대단히 감사합니다! 감사!

+0

"하지만 이제는"어떤 오류가 발생합니까? – krillgar

+0

"IEntityChangeTracker의 여러 인스턴스에서 엔티티 개체를 참조 할 수 없습니다." – MattCucco

+0

데이터베이스에서 가져온 DbContext와 다른 DbContext로 저장하려고하는 것 같습니다. 당신이 모든 반성을 시작하기 전에 아마 당신은 하나를 버렸을까요? – krillgar

답변

0

@krillgar 덕분에 나는 틀린 것을 알아 냈습니다. 여러 문맥을 선언하고 잘못된 컨텍스트로 저장하려고했습니다. 그래서 솔루션은 내 기능을 하나의 기능으로 결합하여 하나의 상황에서 모든 것을 수행했는지 확인하는 것이 었습니다. 그래서 지금 내 함수는 새로운 컨텍스트를 선언하고, 해당 데이터 테이블에서 객체를 가져오고, 모든 검사를 수행하고, 값을 다시 설정하고, 모두 하나의 함수로 저장합니다. 그만큼 간단 해!