0

我已经在邮递员中测试了这个请求(对 Microsoft Dynamics 365 CRM)并且它工作正常:

POST request to https://example.com/api/data/v9.2/foo_module?$select=foo_moduleid 

标头包括:

Prefer:return=representation

身体

{
    foo_AccountId@odata.bind : "/accounts(b770a30d-55d9-e211-89ad-005056ae0100)",
    "foo_name": "Module Name"
}

我不知道如何让 Microsoft.OData.Client 生成此请求。更新记录如下所示,这确实有效,因为我可以使用主键

var moduleQuery = from x in context.foo_modules
                    where x.foo_moduleid == record.CrmId
                    select x;

module = new DataServiceCollection<Foo_module>(moduleQuery).Single();
module.Foo_name = $"Example Online Module (from c# at {DateTime.Now})";

var response = context.SaveChanges(SaveChangesOptions.PostOnlySetProperties);

摘要foo_AccountId@odata.bind : "/accounts(b770a30d-55d9-e211-89ad-005056ae0100)"使用Microsoft.OData.Client时 如何获取body中的属性?

4

1 回答 1

0

解决方案是查询关联的记录(帐户)并将其分配给适当的属性。
这是代码:

var context = _dynamicsContextFactory.CreateContext();

var accountQuery = context.Accounts.Where(x =>
                    x.Primarycontactid.Lastname == lastname
                    && x.Accountnumber == number
                );
var account = new DataServiceCollection<Account>(accountQuery).Single();
var collection = new DataServiceCollection<Foo_module>(context);
var module = new Foo_module();
module.Foo_AccountId = account; // <--- this is the important line
collection.Add(module);
module.Foo_name = $"Example Online Module (from c# at {DateTime.Now})";
var response = context.SaveChanges(SaveChangesOptions.PostOnlySetProperties);
于 2021-05-17T01:15:53.410 回答