我通过在DynamoDBContextConfig中将“IgnoreValue”设置为true来解决此问题。从代码中删除context.SaveAsync(model)。下面是工作代码示例。
[DynamoDBTable("User")] public class UserModel { [DynamoDBHashKey] public int ID { get; set; } [DynamoDBRangeKey] public string Class { get; set; } [DynamoDBProperty] public string Name { get; set; } }
初始化上面的模型之后是将项目插入DynamoDB的代码。
public async Task InsertRecord(UserModel model) { try { using( var context = new DynamoDBContext(client, new DynamoDBContextConfig { ConsistentRead = true, SkipVersionCheck = true,IgnoreNullValues=true })) { var dataContext = context.CreateBatchWrite<UserModel>(); dataContext.AddPutItem(model); await dataContext.ExecuteAsync(); } } catch (Exception ex) { Console.WriteLine(ex.InnerException.StackTrace.ToString()); } }