簡體   English   中英

如何在插件中使用LINQ更新CRM 2011實體?

[英]How to update a CRM 2011 Entity using LINQ in a Plugin?

我們能夠創建沒有任何問題的新實體,但是更新插件中的現有實體似乎不起作用。 這適用於CRM 2011。

var crmContext = new CustomCrmContext(service);

var contact = crmContext.Contact.FirstOrDefault(c=>c.Id == targetEntity.Id);

contact.new_CustomField = "Updated";

crmContext.SaveChanges();

如果您已經擁有Id,則無需下載整個聯系人記錄,只需更新一兩個字段即可。 您也不需要OrganizationServiceContext - 只需要Service。 嘗試類似的東西:

var c = new contact() {
  Id = targetEntity.Id,
  new_CustomField = "Updated"
}

service.Update(c);

這將首先保存查詢聯系人的往返。

您必須將對象標記為已修改,以便將其提交給服務器。 請參閱OrganizationServiceContext.UpdateObject(Entity)

你應該添加crmContext.UpdateObject(contact); crmContext.SaveChanges();之前crmContext.SaveChanges();

LINQ很好,只需創建新對象或列表並在linq中循環列表並更新:

using (var crm = new XrmServiceContext(service)){
var foo = crm.nmipcs_productpriceitemSet
    .Where(ppis => ppis.nmipcs_Account.Id == account.Id).ToList();

foreach (var nmipcsProductpriceitem in foo){
    var f = new nmipcs_productpriceitem
    {
    Id = nmipcsProductpriceitem.Id                 
    ,
    nmipcs_PriceSalesChannel = (decimal) 9.99
    };

    service.Update(f);
}
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM