我正在尝试使用DataServiceCollection使用oData更新D365 Finance and Operation中的Contract中的EmploymentEndDate。 没有错误,似乎一切顺利,但是在Contract EmploymentEndDate中没有任何更改。 类似的代码可以很好地适用于其他实体,例如工人,雇员等...
有人遇到过类似的情况,或者针对此问题提出其他解决方案吗?
谢谢
public bool UpdateContractNG(string pPersonnelNumber, DateTime pEndEmploymentDate)
{
try
{
Resources _contextUpd = Queries365.CreateErpContext();
var queryUpd = from Employment
in _contextUpd.Employments
where Employment.PersonnelNumber == pPersonnelNumber
orderby Employment.EmploymentStartDate descending
select Employment;
DataServiceCollection<Employment> employmentCollection = new DataServiceCollection<Employment>(queryUpd);
if (employmentCollection != null)
{
foreach (Employment emp in employmentCollection)
{
Console.WriteLine($"OLD EMPLOYMENT: {JsonConvert.SerializeObject(emp)}");
emp.EmploymentEndDate = pEndEmploymentDate.ToUniversalTime();
_contextUpd.UpdateObject(emp);
Console.WriteLine($"IN MEMORY CHANGED EMPLOYMENT: {JsonConvert.SerializeObject(emp)}");
break;
}
}
DataServiceResponse response = _contextUpd.SaveChanges(SaveChangesOptions.PostOnlySetProperties);
// for testing (same context Check)
Employment emp2 = Queries365.GetContract(_contextUpd, pPersonnelNumber);
Console.WriteLine($"FROM SAME CONTEXT EMPLOYMENT: {JsonConvert.SerializeObject(emp2)}");
// new context refreshed data
Resources _newContext = Queries365.CreateErpContext();
Employment empAfterNew = Queries365.GetContract(_newContext, pPersonnelNumber);
Console.WriteLine($"AFTER UPDATE NEW CONTEXT: {JsonConvert.SerializeObject(empAfterNew)}");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error updating contract with Id {pPersonnelNumber} with Exception: {ex} ");
return false;
}
}
public static Resources CreateErpContext()
{
// Resource
string ODataEntityPath = ClientConfiguration.Default.UriString + "data";
Uri oDataUri = new Uri(ODataEntityPath, UriKind.Absolute);
Resources context = new Resources(oDataUri);
context.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(delegate (object sender, SendingRequest2EventArgs e)
{
var authenticationHeader = OAuthHelper.GetAuthenticationHeader(useWebAppAuthentication: true);
e.RequestMessage.SetHeader(OAuthHelper.OAuthHeader, authenticationHeader);
});
return context;
}
public static Employment GetContract(Resources context, string personnelNumber)
{
Employment emp = context.Employments.Where(x => x.PersonnelNumber == personnelNumber)
.OrderByDescending(x => x.EmploymentStartDate).FirstOrDefault();
return emp;
}
输出行: Console.WriteLine($“更新新上下文之后{JsonConvert.SerializeObject(empAfterNew)}”); 返回结束日期的意外值:“ EmploymentEndDate”:“ 2019-12-31T23:00:00 + 00:00”。 并且在财务和运营系统D365中未更新