C# TableEntity 在插入操作后保留 IgnoredProperty

标签 c# azure azure-table-storage

我有以下实现 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;

最佳答案

也许是 ExecuteAsync 的问题,它返回与发送的对象相同的对象,而不应用属性, 这是一个解决方法,是一个复制所有属性值的脚本,除了具有特定属性的值

    public static class Helper
    {
        public static MyEntity copyme(this MyEntity entity)
        {
            var copy = new MyEntity();
            var properties = typeof(MyEntity).GetProperties();

            var propertiesToExcept = typeof(MyEntity).GetProperties()
    .Where(prop => prop.IsDefined(typeof(IgnorePropertyAttribute), false));

            foreach (var prop in properties)
            {
                if (!propertiesToExcept.Any(acc => acc.Name == prop.Name))
                {
                    prop.SetValue(copy, prop.GetValue(entity));
                }
            }
            return copy;


        }
    }

为了使用这个,只需调用

                var obj = new MyEntity()
                {
                    MyIgnoredProperty = "toignore",
                    MyProperty = "tokeep"
                };
                var cleanobj = obj.copyme();

关于C# TableEntity 在插入操作后保留 IgnoredProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61771069/

相关文章:

node.js - 语音识别惨败: Wrong status code 401 in Bing Speech API/token

c# - 在 Azure 表存储中筛选日期时间字段时出现异常

c# - 在运行时编译代码,加载到当前 appdomain 但 Type.GetType 看不到它

windows - 我可以使用 Windows XP 进行 Windows Azure 开发吗

c# - 在一组中使用 visual studio 2008 和 2005

azure - 新门户上的 SQL Azure 防火墙规则 (2014)

c# - 使用 Microsoft.Azure.Storage (cosmosDB) 更新表对象失败

Python 使用 azure-data-tables 从 Azure 存储表获取结果

c# - ASP :GridView not getting info from SqlDataAdapter with Stored Procedure

c# - Monotouch 中的 System.ComponentModel.BackgroundWorker - 正确用法?