2011-04-26 1 views
4

자체 추적 엔티티가있는 WCF 데이터 서비스를 개발 중이며 클라이언트가 중복 된 내용을 삽입하지 못하도록하고 싶습니다. 데이터 키에 값을 제공하지 않고 데이터를 POST 할 때마다 데이터가 내 데이터베이스에 이미 존재하는지 여부를 결정하기 위해 일부 논리를 실행해야합니다. 다음과 같이 변경 인터셉터를 작성했습니다.WCF 데이터 서비스 - 레코드를 삽입하는 대신 업데이트합니다.

[ChangeInterceptor("MyEntity")] 
public void OnChangeEntity(MyEntity item, UpdateOperations operations){ 
    if (operations == UpdateOperations.Add) 
    { 
    // Here I search the database to see if a matching record exists. 
    // If a record is found, I'd like to use its ID and basically change an insertion 
    // into an update. 
    item.EntityID = existingEntityID; 
    item.MarkAsModified(); 
    } 
} 

그러나 이것은 작동하지 않습니다. existingEntityID는 무시되고 결과적으로 레코드는 항상 삽입되고 업데이트되지 않습니다. 할 수 있을까요? 미리 감사드립니다.

답변

2

만세! 나는 그것을 할 수 있었다.

item.EntityID = existingEntityID; 
this.CurrentDataSource.ObjectStateManager.ChangeObjectState(item, EntityState.Modified); 

다른 곳에서 개체 상태를 변경해야했습니다. 기본 EntityContext의 속성 인 ObjectStateManager의 .ChangeObjectState를 호출합니다. 나는 .MarkAsModified() 메소드로 오도했다.이 시점에서, 나는 그것이 무엇을하는지 모른다.