2014-10-03 67 views
0

動態CRM插件中的Retrieve和Update之間是否存在關係? 例如,如果我檢索只有一個字段:動態CRM插件中的Retieve和更新之間的關係

Entity e = (Entity)service.Retrieve("EntityLogicalName", EntityGuid, 
new ColumnSet(new string[] {"entityid"})); 

我可以更新實體ë另一場尚未檢索到的? 例如:

e.Attributes["AnotherEntityField1] = "test1"; 
e.Attributes["AnotherEntityField2] = "test2"; 
service.update(e); 

通過不包括在檢索要更新的各個領域,這可能導致一些隱藏的問題是什麼?

回答

2

假設,因爲它的出現,那你只是在檢索實體的主鍵,entityid,你不需要做檢索。

Entity e = new Entity("EntityLogicalName") { Id = EntityGuid }; 
e.Attributes.Add("AnotherEntityField1", "test1"); 
e.Attributes.Add("AnotherEntityField2", "test2"); 
service.Update(e); 

如果你正在做一個檢索確認記錄存在,你需要的try/catch或使用檢索多個因爲Retrieve將拋出一個異常,如果記錄不存在。

1

你想要做的是完全可以接受的,不會造成任何問題。由於您通過Retrieve操作獲取了Entity實例,所以必須正確設置所需的LogicalName和Id才能進行更新。

您的代碼將需要以閱讀下面添加未檢索最初否則你會得到一個KeyNotFoundExceptionEntity型新屬性是剛剛超過Dictionary<string,string>的包裝。

e.Attributes.Add("AnotherEntityField2","test2"); 
0

當您試圖更新實體時,您不需要在屬性收集中存在一個字段,但要避免發生異常「給定的鍵未在字典中顯示」是一種很好的做法,可以先檢查如果屬性收集包含您想要更新的字段。如果是,則更新它,否則您必須將其添加到實體的屬性收集中。

if(e.Attributes.Contains("AnotherEntityField1")) 
{ 
e.Attributes["AnotherEntityField1"] = "test1"; 
} 
else 
{ 
e.Attributes.Add("AnotherEntityField1", "test1"); 
} 

//現在更新操作