停止 Azure 表将字符串视为八进制值
Stop Azure Tables treating strings as octal values
我们已经定义了一个带有字符串 属性 的 Azure Table 对象,我们将在其中存储一个随机的 6 位代码:
SampleTable : TableEntity {
public SampleTable (string partitionKey, string rowKey, string randomCode){
PartitionKey = partitionKey;
RowKey = rowKey;
RandomCode = randomCode;
}
public string RandomCode {get; set;}
}
查看在 Azure Tables 中创建的 table 结构,RandomCode 存储为字符串。
如果我们创建一个随机代码设置为 034120
的模型,Storage Explorer 将存储的值正确显示为 034120
,但是当我们使用以下方法取回值时:
TableOperation retrieveOperation = TableOperation
.Retrieve<SampleTable>(partitionKey, rowKey);
// Execute the retrieve operation.
TableResult retrievedResult = Table.Execute(retrieveOperation);
var result = retrievedResult.Result as SampleTable;
RandomCode的值为102510
(34120的八进制表示)
是否有任何方法可以强制 Azure Tables 将我们的字符串属性视为字符串而不考虑内容?目前我们正在寻求强制我们的随机代码以 1-9 开头,但这似乎是多余的。
作为一个有趣的点,测试其他选项表明存储一个以 0x
开头的值假设该值是十六进制的,并且 returns 是该值的十进制版本,作为一个字符串。如果模型将值视为 int,我可能会理解这一点,但我们将所有内容都视为字符串。
据我所知,Azure table 存储服务和 Azure 存储客户端库不会主动将字符串值转换为八进制值。根据您的描述和代码,我创建了一个示例来重现该问题,该代码在我这边运行良好。您可以参考示例代码来检查您的代码有什么不同。
创建样本表class
public class SampleTable : TableEntity
{
public SampleTable(string partitionKey, string rowKey, string randomCode)
{
PartitionKey = partitionKey;
RowKey = rowKey;
RandomCode = randomCode;
}
public SampleTable() { }
public string RandomCode { get; set; }
}
创建table并插入实体
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("SampleTable");
table.CreateIfNotExists();
SampleTable st = new SampleTable("p001", "pr1", "034120");
TableOperation insertOperation = TableOperation.Insert(st);
table.Execute(insertOperation);
检查存储资源管理器中的实体
检索单个实体
string partitionKey = "p001";
string rowKey = "pr1";
TableOperation retrieveOperation = TableOperation.Retrieve<SampleTable>(partitionKey, rowKey);
TableResult retrievedResult = table.Execute(retrieveOperation);
var result = retrievedResult.Result as SampleTable;
string txt = string.Format("RandomCode: {0}", result.RandomCode.ToString());
If we create a model with the randomcode set to 034120
请与我们分享您用于创建模型的代码。
我们已经定义了一个带有字符串 属性 的 Azure Table 对象,我们将在其中存储一个随机的 6 位代码:
SampleTable : TableEntity {
public SampleTable (string partitionKey, string rowKey, string randomCode){
PartitionKey = partitionKey;
RowKey = rowKey;
RandomCode = randomCode;
}
public string RandomCode {get; set;}
}
查看在 Azure Tables 中创建的 table 结构,RandomCode 存储为字符串。
如果我们创建一个随机代码设置为 034120
的模型,Storage Explorer 将存储的值正确显示为 034120
,但是当我们使用以下方法取回值时:
TableOperation retrieveOperation = TableOperation
.Retrieve<SampleTable>(partitionKey, rowKey);
// Execute the retrieve operation.
TableResult retrievedResult = Table.Execute(retrieveOperation);
var result = retrievedResult.Result as SampleTable;
RandomCode的值为102510
(34120的八进制表示)
是否有任何方法可以强制 Azure Tables 将我们的字符串属性视为字符串而不考虑内容?目前我们正在寻求强制我们的随机代码以 1-9 开头,但这似乎是多余的。
作为一个有趣的点,测试其他选项表明存储一个以 0x
开头的值假设该值是十六进制的,并且 returns 是该值的十进制版本,作为一个字符串。如果模型将值视为 int,我可能会理解这一点,但我们将所有内容都视为字符串。
据我所知,Azure table 存储服务和 Azure 存储客户端库不会主动将字符串值转换为八进制值。根据您的描述和代码,我创建了一个示例来重现该问题,该代码在我这边运行良好。您可以参考示例代码来检查您的代码有什么不同。
创建样本表class
public class SampleTable : TableEntity
{
public SampleTable(string partitionKey, string rowKey, string randomCode)
{
PartitionKey = partitionKey;
RowKey = rowKey;
RandomCode = randomCode;
}
public SampleTable() { }
public string RandomCode { get; set; }
}
创建table并插入实体
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("SampleTable");
table.CreateIfNotExists();
SampleTable st = new SampleTable("p001", "pr1", "034120");
TableOperation insertOperation = TableOperation.Insert(st);
table.Execute(insertOperation);
检查存储资源管理器中的实体
检索单个实体
string partitionKey = "p001";
string rowKey = "pr1";
TableOperation retrieveOperation = TableOperation.Retrieve<SampleTable>(partitionKey, rowKey);
TableResult retrievedResult = table.Execute(retrieveOperation);
var result = retrievedResult.Result as SampleTable;
string txt = string.Format("RandomCode: {0}", result.RandomCode.ToString());
If we create a model with the randomcode set to 034120
请与我们分享您用于创建模型的代码。