您没有提供有关尝试写入 Azure 表存储的实体类型的详细信息,但是如果您的实体包含嵌套的复杂属性,并且您想要写入包括复杂嵌套属性(其本身可能包含嵌套属性)的整个对象图,这些建议的解决方案都不起作用。
我遇到了类似的问题,并实现了一个通用的对象扁平化器/重构器 API,它将把复杂的实体扁平化为扁平化的。EntityProperty
字典并使它们可写入表存储,形式为DynamicTableEntity
.
然后,相同的 API 将重新组合整个复杂对象EntityProperty
字典的DynamicTableEntity
.
看一下:https://www.nuget.org/packages/ObjectFlattenerRecomposer/
我正在与 Azure 团队合作,将此 API 集成到 Azure 存储 SDK 中。您可以在此处查看拉取请求和代码:
https://github.com/Azure/azure-storage-net/pull/337/commits
Usage:
//Flatten object of type Order) and convert it to EntityProperty Dictionary
Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(order);
// Create a DynamicTableEntity and set its PK and RK
DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey);
dynamicTableEntity.Properties = flattenedProperties;
// Write the DynamicTableEntity to Azure Table Storage using client SDK
//Read the entity back from AzureTableStorage as DynamicTableEntity using the same PK and RK
DynamicTableEntity entity = [Read from Azure using the PK and RK];
//Convert the DynamicTableEntity back to original complex object.
Order order = EntityPropertyConverter.ConvertBack<Order>(entity.Properties);
就这样 :)
最新版本的 nuget 包还支持 IEnumerable、ICollection 等类型属性。
该包的 .Net Core 版本在这里:https://www.nuget.org/packages/ObjectFlattenerRecomposer.Core/
CosmosDb Table api 版本的包在这里:https://www.nuget.org/packages/ObjectFlattenerRecomposer.CosmosDb.Table.Core/