尝试在c#中更新Currency字段时出错

本文关键字:Currency 字段 出错 更新 | 更新日期: 2023-09-27 18:08:51

我试图从我的c#代码更新发票字段"运费金额"。我能够很好地更新其他字段,如字符串和Guid,但对于Money,我在创建或更新发票实体时遇到此错误:

There was an error while trying to serialize parameter      
 http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity. The InnerException 
 message was 'Type 'Microsoft.Xrm.Sdk.Money' with data contract name 
'Money:http://schemas.microsoft.com/xrm/2011/Contracts' is not expected. Consider using a 
DataContractResolver or add any types not known statically to the list of known types - 
for example, by using the KnownTypeAttribute attribute or by adding them to the list of 
 known types passed to DataContractSerializer.'.  Please see InnerException for more 
 details.

这是我的一部分代码:

我使用"Microsoft.Xrm.Sdk"的引用

在代码中:

 invoice.Attributes.Add(new KeyValuePair<string, object>("freightamount",  new Microsoft.Xrm.Sdk.Money (row.amount)));

其中row.amount为十进制值。

尝试在c#中更新Currency字段时出错

您是否尝试通过将其添加到web.config中来添加Money作为实体类的KnownTypeAttribute ?

<system.runtime.serialization>
    <dataContractSerializer>
        <declaredTypes>
            <add type="Microsoft.Xrm.Sdk.Entity, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                <knownType type = "Microsoft.Xrm.Sdk.Money, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>                 
             </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>