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

C#TableEntity在插入操作后保持IgnoredProperty,c#,azure,azure-table-storage,C#,Azure,Azure Table Storage,我有以下实现TableEntity的类: public class MyEntity : TableEntity { public string MyProperty { get; set; } [IgnoreProperty] public string MyIgnoredProperty { get; set; } } 然后我有以下代码,它插入一个TableEntity: CloudStorageAccount storageAccount = CloudStor

我有以下实现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也不会在DB中设置。正如所料

...
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;
。。。
CloudStorageAccount-storageAccount=CloudStorageAccount.Parse(“connectionString”);
CloudTableClient tableClient=storageAccount.CreateCloudTableClient();
CloudTable currentTable=tableClient.GetTableReference(tableName);
TableOperation retrieveOperation=TableOperation.Retrieve(partitionKey,rowKey);
TableResult TableResult=等待currentTable.ExecuteAsync(检索操作);
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();

ExecuteAsync可能存在一个问题,即它返回与send相同的对象,而不应用属性, 这是一个解决方案,是一个脚本,它复制除具有特定属性的属性外的所有属性值

    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();
我不是100%确定(因为我无法找到它的代码),但我相信这是由SDK完成的

我相信SDK所做的是在插入实体时,在序列化实体时,通过删除标记为
IgnoreProperty
的属性来创建JSON负载。但是,您的
TableEntity
对象(
entity
在您的示例中)仍然具有此属性

一旦SDK收到响应,它只需更新
实体的
ETag
Timestamp
属性,并将该对象返回到
表Result中。Result

我不是100%确定(因为我无法找到它的代码),但我相信这是由SDK完成的

我相信SDK所做的是在插入实体时,在序列化实体时,通过删除标记为
IgnoreProperty
的属性来创建JSON负载。但是,您的
TableEntity
对象(
entity
在您的示例中)仍然具有此属性


一旦SDK收到响应,它只需更新
实体的
ETag
Timestamp
属性,并将该对象返回到
表Result中。Result

现在奇怪的是insertedEntity包含MyIgnoredProperty,尽管我认为它不应该包含它。
-如果实体包含此属性,您是否签入了表存储?我的猜测是,当您反序列化实体时,它被初始化为默认值。在表存储中,属性没有设置。初始化为默认值也是我首先想到的,但事实并非如此,我检查了它。它与实体具有相同的值。
现在奇怪的是InserteIdentity包含MyIgnoredProperty,尽管我认为它不应该包含它。
-如果实体包含此属性,您是否检查了表存储?我的猜测是,当您反序列化实体时,它被初始化为默认值。在表存储中,属性没有设置。初始化为默认值也是我首先想到的,但事实并非如此,我检查了它。它的值与实体的值相同。这也是我猜的。这也是我猜的