sách gpt4 ai đã đi

EntityState phải được đặt thành null, Đã tạo (đối với thông báo Tạo) hoặc Đã thay đổi (đối với thông báo Cập nhật)

In lại 作者:行者123 更新时间:2023-11-30 17:32:58 33 4
mua khóa gpt4 Nike

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

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 updateEntities)
{
Console.WriteLine("Updating Entities: " + updateEntities.Count);

if (updateEntities.Count > 0)
{
thử
{
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.");
}
khác
{
Log.Debug("Execute multiple executed without errors");
}
}
catch (Exception ex)
{
Log.Error($"Error while executing Multiplerequest", ex);
}
}
}

//下面的 LINQ

private static List GetAllActiveCRMAccounts(CRM2011DataContext CRMcontext)
{

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

thử
{
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 Câu trả lời

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

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

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

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

33 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com