2011-08-16 6 views
1

まずは、ありがとうございました。私はエンティティを挿入/更新/削除し、それらの操作の履歴を保存するためにriaサービスを使用しています。私は操作を実行し、サービスへの1回の呼び出しで履歴を保存したいと思います。私は挿入に生成されている新しいエンティティIDが必要なので、今は挿入に固執しています。私はすべて一緒に間違ったアプローチを取っているかもしれませんが(私はそうではないと思います)。私は、提出方法をオーバーライドしている、と履歴テーブルにスナップショットを保存しようとしています、私は、元のバージョンのスナップショットを保存しない:ここではRIAサービス+エンティティフレームワークサーバー側挿入更新削除 - ログ

public override bool Submit(ChangeSet changeSet) 
    { 
     //SUBMIT FIRST SO THE OBJECT(S) HAVE AN ID 
     var success = base.Submit(changeSet); 
     if (success) 
      foreach (var changeSetEntry in changeSet.ChangeSetEntries) 
      { 
       if (changeSetEntry.Entity is MyBusinessEntity) 
       { 
        var newBusinessEntity = (MyBusinessEntity) changeSetEntry.Entity; 
        RecordModifiedMyBusinessEntity(changeSetEntry.Operation, newBusinessEntity); 
       } 
      } 
     return success; 
    } 

    private void RecordModifiedMyBusinessEntity(DomainOperation operation, MyBusinessEntity newBusinessEntity) 
    { 
     var hist = new BusinessEntityHistory 
     { 
      ChangedBy = new AuthenticationService().GetUser().FriendlyName, 
      ChangedDate = DateTime.Now, 
      Operation = operation.ToString(), 
      BusinessEntityId = newBusinessEntity.Id, 
      Group = newBusinessEntity.Group, 
      Priority = newBusinessEntity.Priority, 
      .... 
     }; 
     InsertBusinessEntityHistory(hist); 
     //HERE IS WHERE I WANT TO CALL SUBMIT CHANGES AGAIN, BUT 1 - IT'S NOT IN THE CHANGESET, 
     //AND 2 - THE OBJECT I ALREADY INSERTED IS IN THE CHANGESET (SO IF I SUBMIT AGAIN, IT GETS 
     //INSERTED TWICE AND NO HISTORY IS SAVED. AND 3 - I CAN'T DO THE HISTORY BEFORE BECAUSE I DON'T 
     //HAVE THE ID, AND I DON'T WANT TO DO A MAX ID + 1 BECAUSE SOMEONE ELSE MIGHT BE 
     //INSERTING INTO THE SAME TABLE 
    } 

答えて

2

は、私が一緒に行くことになったソリューションです。

public override bool Submit(ChangeSet changeSet) 
    { 
     //submit the changes 
     var success = base.Submit(changeSet); 
     if (success) 
     { 
      //make a new list of change set entries 
      var entries = new List<ChangeSetEntry>(); 
      //each change set entry needs an id (not to be confused with the entity's id) 
      var maxId = 0; 
      //iterate through each change and add historical snapshot. 
      foreach (var changeSetEntry in changeSet.ChangeSetEntries) 
      { 
       var entity = changeSetEntry.Entity; 
       var operation = changeSetEntry.Operation; 
       var myEntity = entity as MyEntityType; 
       if (myEntity != null) 
       { 
        entries.Add(GetHistoryChangeSetEntry(ref maxId, operation, myEntity)); 
        continue; 
       } 
      } 
      //make new change set with historical snapshots 
      var newChangeSet = new ChangeSet(entries); 
      //submit the new change set 
      base.Submit(newChangeSet); 
     } 
     return success; 
    } 

    private ChangeSetEntry GetHistoryChangeSetEntry(ref int maxId, DomainOperation operation, MyEntityType myEntity) 
    { 
     return new ChangeSetEntry 
       { 
        Id = ++maxId, 
        //We are inserting this change set entry 
        Operation = DomainOperation.Insert, 
        Entity = new MyEntityTypesHistory 
           { 
            ChangedBy = ServiceContext.User.Identity.Name, 
            ChangedDate = DateTime.Now, 
            //The operation performed on the original entity 
            Operation = operation.ToString(), 
            MyEntityId = myEntity.EntityId, 
            MyEntityField1 = myEntity.EntityField1, 
            MyEntityField2 = myEntity.EntityField2 
           } 
       }; 
    } 

新しい変更セットを作成し、新しい変更セットエントリを作成し、新しい変更セットで変更を送信する必要がありました。

関連する問題