如何让 Microsoft.OData.Client 在更新时关联表?

How do I get Microsoft.OData.Client to Associate tables on update?

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

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

Headers包括:

Prefer:return=representation

Body

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

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

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

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