DynamoDB-使用AWS SDK for.NET映射任意C#数据类型

DynamoDB-使用AWS SDK for.NET映射任意C#数据类型,c#,asp.net-core,amazon-dynamodb,aws-sdk,C#,Asp.net Core,Amazon Dynamodb,Aws Sdk,我试图将一个复杂的C#对象持久化到DynamoDB中,它看起来像这样 [DynamoDBTable(“some_table”)] 公共类用户 { [DynamoDBHashKey] 公共字符串用户_id{get;set;} [动态键] 公共字符串客户端名称{get;set;} 公共字符串客户端位置{get;set;} 公共日期时间注册\u日期{get;set;} [发电机性能(类型(KycConverter))] 公共KycAttributes kyc_属性{get;set;} } 公共类Kyc

我试图将一个复杂的C#对象持久化到DynamoDB中,它看起来像这样

[DynamoDBTable(“some_table”)]
公共类用户
{
[DynamoDBHashKey]
公共字符串用户_id{get;set;}
[动态键]
公共字符串客户端名称{get;set;}
公共字符串客户端位置{get;set;}
公共日期时间注册\u日期{get;set;}
[发电机性能(类型(KycConverter))]
公共KycAttributes kyc_属性{get;set;}
}
公共类KycAttributes
{
公共字符串kyc_id{get;set;}
}
KycAttributes类型属性需要转换为DynamoDB接受的格式。以下是遵循本指南的转换器实现:

公共类KycConverter:IPropertyConverter
{
来自条目的公共对象(DynamoDBEntry条目)
{
基元=作为基元的条目;
if(primitive==null)返回新的KycAttributes();
if(primitive.Type!=DynamoDBEntryType.String)
{
抛出新的InvalidCastException(string.Format(“KycAttributes无法转换,因为其类型为{0},值为{1})”
,primitive.Type,primitive.Value));
}
字符串json=primitive.AsString();
返回JsonConvert.DeserializeObject(json);
}
公共发电机输入(对象值)
{
Trace.TraceInformation(“调用”);
KycAttributes attributes=作为KycAttributes的值;
如果(attributes==null)返回null;
字符串json=JsonConvert.SerializeObject(属性);
返回新原语(json);
}
}
然后,当调用
DynamoDBContext.SaveAsync
时,将引发一个异常,该异常具有以下stacktrace:

Object reference not set to an instance of an object.
at Amazon.DynamoDBv2.Model.Internal.MarshallTransformations.AttributeValueMarshaller.Marshall(AttributeValue requestObject, JsonMarshallerContext context) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Generated\Model\Internal\MarshallTransformations\AttributeValueMarshaller.cs:line 48
   at Amazon.DynamoDBv2.Model.Internal.MarshallTransformations.UpdateItemRequestMarshaller.Marshall(UpdateItemRequest publicRequest) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Generated\Model\Internal\MarshallTransformations\UpdateItemRequestMarshaller.cs:line 165
   at Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\Marshaller.cs:line 79
   at Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\Marshaller.cs:line 51
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\CallbackHandler.cs:line 60
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.DynamoDBv2.DocumentModel.Table.UpdateHelperAsync(Document doc, Key key, UpdateItemOperationConfig config, CancellationToken cancellationToken) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Custom\DocumentModel\Table.cs:line 871
   at Amazon.DynamoDBv2.DataModel.DynamoDBContext.SaveHelperAsync[T](T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Services\DynamoDBv2\Custom\DataModel\Context.cs:line 285

请注意,场景如下:这是数据库中已经存在的对象。但是,该对象没有kyc_属性键,正在尝试将该键添加到该对象中。这可能吗?如果是这样,如何修复错误?

您找到解决方案了吗?@vishwadi我没有。我不得不放弃这种方法,用共享的
user\u id
属性创建一个新的映射表<代码>kyc_id已添加到其中。因此,要获取所有数据,现在必须查询两个表,并根据结果生成DTO。