gpt4 book ai didi

c# - EntityState 必须设置为 null、Created(对于 Create 消息)或 Changed(对于 Update 消息)

转载 作者:行者123 更新时间:2023-11-30 17:32:58 31 4
gpt4 key购买 nike

在我的 C# 控制台应用程序中,我正在尝试更新 CRM 2016 中的帐户。IsFaulted 不断返回 true。当我向下钻取时它返回的错误消息如下:

EntityState must be set to null, Created (for Create message) or Changed (for Update message).

另外,为了防止它可能导致错误,我在底部粘贴了我的 LINQ 查询。

我从 Google 得到的答案表明我正在混合使用 ServiceContextProxyService(这不是,我没有在这种情况下使用它)。其他人说我使用的 context.UpdateObject(object) 不正确,我也没有使用。

更新:刚刚有人告诉我上面的错误是因为我试图返回所有的元数据而不仅仅是更新后的数据。我仍然不知道如何修复错误,但这些信息应该会有所帮助。

private static void HandleUpdate(IOrganizationService crmService, List<Entity> updateEntities)
{
Console.WriteLine("Updating Entities: " + updateEntities.Count);

if (updateEntities.Count > 0)
{
try
{
var multipleRequest = new ExecuteMultipleRequest()
{
// Assign settings that define execution behavior: continue on error, return responses.
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = true,
ReturnResponses = true
},
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection()
};

foreach (var account in updateEntities)
{
multipleRequest.Requests.Add(
new UpdateRequest()
{
Target = account
});
}

ExecuteMultipleResponse response = (ExecuteMultipleResponse)crmService.Execute(multipleRequest);

if (response.IsFaulted)
{
int failedToUpdateAccount = 0;
foreach (ExecuteMultipleResponseItem singleResp in response.Responses)
{
if (singleResp.Fault != null)
{
string faultMessage = singleResp.Fault.Message;
var account = ((UpdateRequest)multipleRequest.Requests[singleResp.RequestIndex]).Target;

Log.Error($"Error update acc.id: {account.Id}.Error: {singleResp.Fault.Message}.");
failedToUpdateAccount++;
}
}
Log.Debug($"Failed to update {failedToUpdateAccount} accounts.");
}
else
{
Log.Debug("Execute multiple executed without errors");
}
}
catch (Exception ex)
{
Log.Error($"Error while executing Multiplerequest", ex);
}
}
}

//下面的 LINQ

private static List<Account> GetAllActiveCRMAccounts(CRM2011DataContext CRMcontext)
{

Console.WriteLine("Start Getting CRMExistingAccounts ....");
List<Account> CRMExisterendeAccounts = new List<Account>();

try
{
CRMExisterendeAccounts = (from a in CRMcontext.AccountSet
where a.StateCode == AccountState.Active
where a.anotherVariable == 1
select new Account()
{
my_var1 = a.myVar1,
my_var2 = a.myVar2,
AccountId = a.AccountId,
anotherVar = a.alsoThisVar,
}).ToList();

}
catch (FaultException ex)
{
Log.Debug($"GetCRMExistingAccounts Exception { ex.Message}");
Console.WriteLine("GetCRMExistingAccounts Exception " + ex.Message);
throw new Exception(ex.Message);
}

return CRMExisterendeAccounts;
}

是的,我的变量在我的系统中有不同的名称。查询返回包含所有正确数据的对象。

最佳答案

您可以通过以下两种方式之一解决此问题:

1) 创建 CRM2011DataContext 并将 MergeOption 设置为 MergeOption.NoTracking。从不跟踪的上下文加载的实体将具有空的 EntityState 属性。

2) 您可以创建实体的副本并保存副本。

关于c# - EntityState 必须设置为 null、Created(对于 Create 消息)或 Changed(对于 Update 消息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45188428/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com