我已经使用OData V4 Client Code Generator为Visual Studio 2017生成了OData客户端代码。当我调用save更改时,字段值不包含在JSON有效负载中。谁能帮助我理解为什么?
客户端代码是从Dynamics CRM Web API端点https://.api.crm.dynamics.com/api/data/v9.1/生成的。
当我调用context.SaveChanges();时,将生成完整的有效负载。而不设置SaveChangesOptions.PostOnlySetProperties选项,但这会导致其他问题,例如ownerid不能为null。
class Program
{
public static void Main(string[] args)
{
Poc.D365Crm.OData.Microsoft.Dynamics.CRM.Contact contact = null;
try
{
var context = new Poc.D365Crm.OData.Microsoft.Dynamics.CRM.System(new Uri("https://<domain>.api.crm.dynamics.com/api/data/v9.1/"));
context.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(delegate (object sender, SendingRequest2EventArgs e)
{
e.RequestMessage.SetHeader("Authorization", CreateAuthenticationHeader());
});
var accounts = context.Accounts.Take(1).ToList();
contact = new Poc.D365Crm.OData.Microsoft.Dynamics.CRM.Contact()
{
Firstname = "Fred",
Lastname = "Flintstone",
Birthdate = DateTime.Now.AddYears(-13),
//Parentcustomerid_account = accounts[0],
Emailaddress1 = "ff@ff.com",
Contactid = Guid.NewGuid()
};
var contactCollection = new DataServiceCollection<Contact>(context);
contactCollection.Add(contact);
var response = context.SaveChanges(SaveChangesOptions.PostOnlySetProperties);
Log("Success!", ConsoleColor.Green);
}
catch (Exception ex)
{
}
}
public static void Log(string message, ConsoleColor colour)
{
var originalColour = Console.ForegroundColor;
Console.ForegroundColor = colour;
Console.WriteLine(message);
Console.ForegroundColor = originalColour;
}
public static string CreateAuthenticationHeader()
{
string aadTenant = "https://login.windows.net/<domain>";
string aadClientAppId = "app id guid";
string aadClientAppSecret = "secret";
string aadResource = "https://<domain>.crm.dynamics.com";
AuthenticationContext authenticationContext = new AuthenticationContext(aadTenant, false);
var creadential = new ClientCredential(aadClientAppId, aadClientAppSecret);
var authenticationResult = authenticationContext.AcquireTokenAsync(aadResource, creadential).Result;
return authenticationResult.CreateAuthorizationHeader();
}
}
}