我正在使用Microsoft.PowerPlatform.Dataverse.Client库使用 .Net 核心应用程序获取数据并将其创建到我的动态 365 环境中。我正在尝试使用此方法在同一请求中创建一个包含联系人的帐户。
它工作得很好,但我无法设法将联系人“parentcustomerid”链接到我刚刚创建的帐户,同时仍然只执行一个请求。
我发现了一种在创建后进行更新的解决方法,但这与我只想执行一个请求的事实背道而驰(执行时间确实需要保持尽可能低)。有解决方法代码:
var account = new Entity(this._options.Account.Name);
account[this._options.Account.Columns.AccountName] = accountName;
var contact = new Entity(this._options.Contact.Name);
contact[this._options.Contact.Columns.FirstName] = dataverseContact.FirstName;
contact[this._options.Contact.Columns.LastName] = dataverseContact.LastName;
contact[this._options.Contact.Columns.Email] = dataverseContact.Email;
var contactCollection = new EntityCollection();
contactCollection.Entities.Add(contact);
account.RelatedEntities[new Relationship("aci_account_contact_AccountId")] = contactCollection;
var accountId = this._serviceClient.Create(account);
var contactId = GetContactIdByEmail(dataverseContact.Email).Result;
var updatedContact = new Entity(this._options.Contact.Name);
updatedContact.Id = contactId;
updatedContact[this._options.Contact.Columns.CompanyName] = new EntityReference(this._options.Account.Name, accountId);
this._serviceClient.UpdateAsync(updatedContact);
但是,我想要这样的东西(这不起作用,因为在创建它之前我没有 accountId ......):
var account = new Entity(this._options.Account.Name);
account[this._options.Account.Columns.AccountName] = accountName;
var contact = new Entity(this._options.Contact.Name);
contact[this._options.Contact.Columns.FirstName] = dataverseContact.FirstName;
contact[this._options.Contact.Columns.LastName] = dataverseContact.LastName;
contact[this._options.Contact.Columns.Email] = dataverseContact.Email;
// New line that is not working because I don't have the accountId
contact["parentcustomerid"] = new EntityReference(this._options.Account.Name, accountId);
var contactCollection = new EntityCollection();
contactCollection.Entities.Add(contact);
account.RelatedEntities[new Relationship("aci_account_contact_AccountId")] = contactCollection;
var accountId = this._serviceClient.Create(account);
我想知道是否有解决方案,或者我每次都坚持做 Get 然后更新?