【问题标题】:Why is my int being read as 0 from Azure Table Storage, not as the stored value?为什么我的 int 从 Azure 表存储中读取为 0,而不是存储值?
【发布时间】:2019-02-04 16:34:05
【问题描述】:

我正在尝试读取存储在 Azure 存储表中的值。当我在 Fiddler 中检索实体时,我可以看到响应具有正确的值,即:

{
"odata.etag":"W/\"datetime'2019-01-24T20%3A23%3A58.3605274Z'\"",
"PartitionKey":"0029c461-74b9-47a7-955b-28e07b1905ab",
"RowKey":"09a79860-f568-481c-9641-9a1d53ebd678",
"Timestamp":"2019-01-24T20:23:58.3605274Z",
"AdjustmentId":"[GUID]",
"CompanyName":"[CompanyNme]",
"CustomerId":"[GUID]",
"Date@odata.type":"Edm.DateTime",
"Date":"2019-01-24T20:23:49.9487324Z",
"FinalQuantity":35,
"Id":"[GUID]",
"InitialQuantity":36.0,
"OfferName":"[Product]",
"Requester":"[User]",
"Status":"Accepted",
"StatusMessage":"Created",
"Type":"QuantityAdjustment"
}

但是,当我在 C# 代码中使用此响应时,InitialQuantity 设置为 0。我使用的是 Microsoft Azure WebJobs Extensions Storage v3.0.3 和 WindowsAzure.Storage v9.3.3。

Azure 存储 SDK 映射到的实体类如下:

public class Transaction : TableEntity, ITransaction
    {
/// <summary>
        /// Gets or sets the adjustment identifier.
        /// </summary>
        /// <value>The adjustment identifier.</value>
        public string AdjustmentId { get; set; }

        /// <summary>
        /// Gets or sets the name of the company.
        /// </summary>
        /// <value>The name of the company.</value>
        public string CompanyName { get; set; }

        /// <summary>
        /// Gets or sets the customer identifier.
        /// </summary>
        /// <value>The customer identifier.</value>
        public string CustomerId { get; set; }

        /// <summary>
        /// Gets or sets the Transaction's date. Should be generated on creation.
        /// </summary>
        /// <value>The date.</value>
        public DateTime? Date { get; set; }

        /// <summary>
        /// Gets or sets the final quantity following the transaction.
        /// </summary>
        /// <value>The final quantity.</value>
        public int FinalQuantity { get; set; }

        /// <summary>
        /// Gets or sets the transaction identifier. Should be a GUID in string format. This is
        /// generated when a new Transaction is created.
        /// </summary>
        /// <value>The identifier.</value>
        public string Id { get; set; } = Guid.NewGuid().ToString().ToLower();

        /// <summary>
        /// Gets or sets the initial quantity.
        /// </summary>
        /// <value>The initial quantity.</value>
        public int InitialQuantity { get; set; }

        /// <summary>
        /// Gets or sets the offer identifier.
        /// </summary>
        /// <value>The offer identifier.</value>
        public string OfferId { get; set; }

        /// <summary>
        /// Gets or sets the name of the offer.
        /// </summary>
        /// <value>The name of the offer.</value>
        public string OfferName { get; set; }

        /// <summary>
        /// Gets or sets the requester.
        /// </summary>
        /// <value>The requester.</value>
        public string Requester { get; set; }

        /// <summary>
        /// Gets or sets the status. Set to pending by default.
        /// </summary>
        /// <value>The status.</value>
        public string Status { get; set; } = TransactionStatus.Pending;

        /// <summary>
        /// Gets or sets the status message.
        /// </summary>
        /// <value>The status message.</value>
        public string StatusMessage { get; set; }

        /// <summary>
        /// Gets or sets the type. Should be set using the constants in the <see
        /// cref="T:LicenseAdjustmentManagement.Infrastructure.TransactionTypes"/> class.
        /// </summary>
        /// <value>The type.</value>
        public string Type { get; set; }

public string ToJson()
        {
            return JsonConvert.SerializeObject(this);
        }
}

调用实体的代码。

var storageAccount = CloudStorageAccount.Parse("[ConnectionString]");
var tableClient = storageAccount.CreateCloudTableClient();
var transactionsTable = tableClient.GetTableReference("Transactions");

var query = TableQuery<Transaction>().Where(TableQuery.GenerateFilterCondition("Id", QueryComparisons.Equal, "[GUID]"));

var results = await transactionsTable.ExecuteQuerySegmentedAsync(query, default(TableContinuationToken)).AsEnumerable();

var transaction = results.First();

所有其他值都被正确读取,但 InitialValue 始终为 0。有什么建议吗?

编辑:正如下面 KSib 所建议的,InitialValue 被序列化为 decimaldouble,因此当它被反序列化为 int 时,它会收到 int 的默认值 0。知道为什么这个东西在声明为int 时被序列化为decimal 吗?

【问题讨论】:

  • in my C# code 你忘了添加你的代码

标签: c# azure azure-table-storage


【解决方案1】:

您可能正在尝试将小数反序列化为 int,而不是抛出异常,我假设它只是将属性设置为 default(int),即 0

【讨论】:

  • 嗯,有道理。甚至没有注意到初始数量中的小数点。现在我必须弄清楚为什么它首先被序列化为小数,因为模型提供了一个 int。
猜你喜欢
  • 1970-01-01
  • 2017-09-15
  • 2019-12-07
  • 2014-10-25
  • 2016-06-19
  • 2015-11-13
  • 2019-11-17
  • 1970-01-01
  • 2020-11-12
相关资源
最近更新 更多