首先,谢谢你的帮助。我正在使用ria服务来插入/更新/删除实体并保存这些操作的历史记录。我想执行操作并将历史保存在一次调用服务中。现在我被卡在插入上因为我需要在插入上生成的新实体ID。我可能会采取错误的方法(但我希望不是)。我已经覆盖了提交方法,并且我试图在历史表中保存快照,我不想保存原始版本的快照:
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
}
答案 0 :(得分: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
}
};
}
我必须为其创建一个新的更改集和新的更改集条目,并使用新的更改集提交更改。