在 dynamics 的一次操作中创建相关实体时,有没有办法 link 联系 "parentcustomerid" 进行帐户?

Is there a way to link contact "parentcustomerid" to account when creating related entities in one operation in dynamics?

我正在使用 Microsoft.PowerPlatform.Dataverse.Client library to get and create data into my dynamics 365 env using a .Net core app. I'm trying to create an Account with a Contact in the same request using this method

效果很好,但我无法设法将联系人“parentcustomerid”link 发送到我刚刚创建的帐户,同时仍然只执行一个请求。

我找到了一种在创建后进行更新的解决方法,但这违背了我只想执行一个请求的事实(执行时间确实需要尽可能短)。有解决方法代码:

            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);

我想知道是否有解决这个问题的方法,或者我每次都坚持先获取然后再更新?

如果创建 entity 的新实例,它没有 id,因为直到您 运行 才在动态中创建它。 Entity 的实例仅位于您的内存中。

Guid accountId = this._serviceClient.Create(account)

到 link 联系人的帐户,您需要帐户的 GUID。 因此,在您 linkt 实体之前,您必须调用

Guid accountId = this._serviceClient.Create(account)

您必须自己生成一些 Guid,但如果您这样做了,这是可能的。

查看来自 Microsoft 的示例:https://github.com/microsoft/PowerApps-Samples/blob/1adb4891a312555a2c36cfe7b99c0a225a934a0d/cds/orgsvc/C%23/CreateUpdateRecordsWithRelatedRecords/CreateUpdateRecordsWithRelatedRecords/SampleProgram.cs