2012-05-17 5 views
2

RavenDB (빌드 # 888)에 엔티티가 저장되어 있습니다. 새로운 버전의 응용 프로그램과 연결된 데이터베이스 마이그레이션으로 인해 ID를 변경해야합니다. 이 코드의 결과RavenDB에 저장된 데이터의 마이그레이션

public bool Migrate() 
{ 
    using (var session = _store.OpenSession()) 
    { 
     var users = session.Query<User>().ToList(); 
     foreach (var user in users) 
     { 
      user.Id = user.Email; 
     } 
     session.SaveChanges(); 
    } 
} 

실행 예외 : 이

Status: System.InvalidOperationException: Entity App.Models.Entities.User had document key 'mydomain-mylogin' but now has document key property '[email protected]'. 
You cannot change the document key property of a entity loaded into the session 
    at Raven.Client.Document.InMemoryDocumentSessionOperations.CreatePutEntityCommand(Object entity, DocumentMetadata documentMetadata) 
    at Raven.Client.Document.InMemoryDocumentSessionOperations.PrepareForEntitiesPuts(SaveChangesData result) 
    at Raven.Client.Document.InMemoryDocumentSessionOperations.PrepareForSaveChanges() 
    at Raven.Client.Document.DocumentSession.SaveChanges() 

방법이

을 수행 할 수 있습니다 나는 내가 사용하고 샘플 기능을 작성했습니다

?

답변

3

나의 현재 솔루션은 데이터베이스와 같은 새로운 개체를 저장, 원하는 속성을 변경, 일시적인 것들에 현재 개체를 복사하고, 마지막으로 낡은 제거한다 : 그것은으로

using (var session = _store.OpenSession()) 
{ 
    var users = session.Query<User>().ToList(); 
    foreach (var user in users) 
    { 
     var newuser = new User 
          { 
           Id = user.Email, 
           DisplayName = user.DisplayName 
          }; 

     session.Store(newuser); 
     session.Delete(user); 
    } 
    session.SaveChanges(); 
} 
+1

ID 필드는 문서의 중요한 부분입니다 그것을 찾는 데 사용되는 키. 그래서 그것이 당신이 그것을 바꿀 수없는 이유입니다. –

+2

새 문서를 만든 다음 이전 문서를 삭제하는 것이 가장 쉬운 방법입니다. –

+0

감사합니다. 몇 분 전에 그 일을 해낸 것 같습니다. 문안 인사. – jwaliszko

관련 문제