2011-12-23 3 views
1

그럼에도 재미있는 일이 있습니다.하나의 조직에서 다른 조직으로 깊은 복제 엔티티

2 개의 CRM 2011 조직간에 일부 레코드가 동기화되도록 Windows 서비스를 만들었습니다.

우리는 원하는 복제본을 살펴 봤지만 실제 엔터티가 아니라 EntityReference를 복제하는 것으로 보입니다.

모든 아이디어, 조언 등은 훌륭합니다.

답변

1

동적 인 경우에는 사용하지 않지만 깊은 복사가 필요한 경우에는 BinaryFormatter를 사용하여 개체를 serialize 한 다음이를 새 개체로 역 직렬화합니다. 이는 Net Remoting의 경우와 매우 유사합니다.

''' <summary> 
''' This method clones all of the items and serializable properties of the current collection by 
''' serializing the current object to memory, then deserializing it as a new object. This will 
''' ensure that all references are cleaned up. 
''' </summary> 
''' <returns></returns> 
''' <remarks></remarks> 
Public Function CreateSerializedCopy(Of T)(ByVal oRecordToCopy As T) As T 
    ' Exceptions are handled by the caller 

    If oRecordToCopy Is Nothing Then 
     Return Nothing 
    End If 

    If Not oRecordToCopy.GetType.IsSerializable Then 
     Throw New ArgumentException(oRecordToCopy.GetType.ToString & " is not serializable") 
    End If 

    Dim oFormatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter 

    Using oStream As IO.MemoryStream = New IO.MemoryStream 
     oFormatter.Serialize(oStream, oRecordToCopy) 
     oStream.Position = 0 
     Return DirectCast(oFormatter.Deserialize(oStream), T) 
    End Using 
End Function 
+0

답장을 보내 주셔서 감사합니다. serialize/de-serialize 라우트는 나에게 잘 들린다. 예제를 제공해 주시겠습니까? "심층적 인"방법을 사용하면 복제 할 때이를 알 수 있고 직렬화 된 형식에서 다른 조직으로 가져 오는 방법을 알 수 있습니다. 감사. – user1231231412

+0

@JonC : 예제로 VB 코드를 추가했습니다. 전체 개체 트리를 serialize하지만 각 개체의 serialization 생성자 및 GetData 메서드를 호출하므로 필요에 따라 정보를 변경할 수 있습니다. 우리가 CRM을 사용하지 않기 때문에 변화하는 조직에 관련된 것이 무엇인지 완전히 명확하지는 않지만 이것이 일반적인 .net 객체에 사용하는 솔루션입니다. –

2

나는이 코드를 성공적으로 깊은 복제입니다 :

여기 (원하는 경우 내가 C 번호로 변환 할 수 있습니다) 우리의 VB.Net 솔루션입니다

나는 xRM을 서비스 컨텍스트를 사용하여 복사 새 엔티티에 복제하고 기존 엔티티와의 관계를 설정 한 기존 엔티티 (예 : 기본 키 등)로 지정하는 속성을 제거한 다음 create 메소드에 보냅니다. 모든 관련 엔티티에 대해 명시 적으로 동일한 작업을 수행해야합니다 (예제에서 웹 파일과 노트에 대해 수행 한 작업).

 var clonedwebpage = ((Entity)webpage).Clone(true); 

     clonedwebpage.Id = Guid.NewGuid(); 
     clonedwebpage.Attributes.Remove("adx_webpageid"); 
     //clonedwebpage.Attributes.Remove("adx_pagetemplateid"); 
     clonedwebpage.EntityState = null; 
     clonedwebpage["adx_websiteid"] = new EntityReference("adx_webpage",livesiteGuid); 
     //clonedwebpage["adx_parentpageid"] = 
     // create the template guid 
     Guid tempGuid = new Guid(templateguid); 
     clonedwebpage["adx_pagetemplateid"] = new EntityReference("adx_pagetemplate",tempGuid); // set the template of the new clone 


     //serviceContext.Attach(cloned); 
     //serviceContext.MergeOption = MergeOption.NoTracking; 

     Guid clonedwebpageguid = _service.Create(clonedwebpage); 

     //var webpage = serviceContext.Adx_webpageSet.Where(wp => wp.Id == webpageguid).First(); 
     //var notes_webile = serviceContext.Adx_webfileSet.Where(wf => wf. 








     //*********************************** WEB FILE ********************************************* 
     foreach (var webfile in webpage.adx_webpage_webfile) 
     { 
      var cloned_webfile = webfile.Clone(); 

      //should iterate through every web file that is related to a web page, and clone it. 
      cloned_webfile.Attributes.Remove("adx_webfileid"); 
      cloned_webfile.Attributes.Remove("adx_websiteid"); 
      cloned_webfile.Attributes.Remove("adx_parentpageid"); 
      cloned_webfile["adx_websiteid"] = new EntityReference("adx_website", livesiteGuid); 
      cloned_webfile["adx_parentpageid"] = new EntityReference("adx_webpage", clonedwebpageguid); 
      cloned_webfile.EntityState = null; 
      cloned_webfile.Id = Guid.NewGuid(); 
      Guid ClonedWebFileGuid = _service.Create(cloned_webfile); 

      //*********************************** NOTE ********************************************* 
      foreach (var note in webfile.adx_webfile_Annotations) 
      { 

       var cloned_note = note.Clone(); 

       cloned_note.Attributes.Remove("annotationid"); // pk of note 
       cloned_note.Attributes.Remove("objectid"); // set to web file guid 

       cloned_note["objectid"] = new EntityReference("adx_webfile", ClonedWebFileGuid); // set the relationship between our newly cloned webfile and the note 
       cloned_note.Id = Guid.NewGuid(); 
       cloned_note.EntityState = null; 

       Guid clonednote = _service.Create(cloned_note); 


       //cloned_note.Attributes.Remove("ownerid"); 
       //cloned_note.Attributes.Remove("owningbusinessunit"); 
       //cloned_note.Attributes.Remove("owningteam"); 
       //cloned_note.Attributes.Remove("owninguser"); 


      } 
관련 문제