【发布时间】:2020-05-13 09:33:24
【问题描述】:
我有以下实现 TableEntity 的类:
public class MyEntity : TableEntity
{
public string MyProperty { get; set; }
[IgnoreProperty]
public string MyIgnoredProperty { get; set; }
}
然后我有以下代码,插入一个 TableEntity:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connectionString");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
ITableEntity entity = new MyEntity ();
entity.RowKey = "row";
entity.PartitionKey = "partition";
entity.MyIgnoredProperty = "ignored";
CloudTable currentTable = tableClient.GetTableReference("MyEntity");
TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
TableResult result = await currentTable.ExecuteAsync(insertOrMergeOperation);
var insertedEntity = result.Result as ITableEntity;
现在奇怪的是 insertedEntity 包含 MyIgnoredProperty,尽管我希望它不应该包含它。
有人可以解释为什么它会这样吗?
顺便说一句,如果我使用以下代码显式检索实体,则 MyIgnoredProperty 也未设置在数据库中。正如预期的那样。
...
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connectionString");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable currentTable = tableClient.GetTableReference(tableName);
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
TableResult tableResult = await currentTable.ExecuteAsync(retrieveOperation);
T result = (T)tableResult.Result;
【问题讨论】:
-
Now the strange part is that insertedEntity contains the MyIgnoredProperty, although I would expect that it shouldn't contain it.- 如果实体包含此属性,您是否检查过表存储?我的猜测是,当您反序列化实体时,它会被初始化为默认值。 -
在 TableStorage 中没有设置属性。初始化为默认也是我最先想到的,但事实并非如此,我查了一下。它与实体具有相同的值。
标签: c# azure azure-table-storage