我将guid存储在我的azure表存储中,但类型为 在azure表存储门户中显示为字符串而不是guid。
我正在使用带有api令牌的rest api将实体存储在azure表存储中。
{
public Guid id {get;set;}
public string name {get;set;}
}
string sastoke = GetSasToken(); // no issues here
string url = config.table_storage_url + sastoken // no issues here
// assuming i already have rowkey and partition key
myClass entity = new myClass{id = Guid.NewGuid();name = "abcdef";}
var content = new StringContent(JsonConvert.SerializeObject(entity), Encoding.UTF8, "application/json");
// creating http client
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsync(url, content);
return response;
}
预期:ID的类型应在Azure表存储门户中引导。
实际:id类型在azure表存储门户中显示字符串。
答案 0 :(得分:0)
按照以下doc的规定,当您使用azure表存储rest api插入数据时,
默认情况下,除非您指定其他类型,否则属性将创建为String类型。
如果要指定显式类型,则格式应如下所示(doc here):
因此,在使用rest api时,您需要为该属性明确指定类型(例如Edm.Guid
)。另外请注意,如果您使用的是SDK,则不会出现此类问题。
这是一个简单的代码(也许有一些更好的方法来指定类型,但这只是一个解释):
实体类:
public class MyClass
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Name { get; set; }
public Guid TestId { get; set; }
}
主要方法:
static void Main(string[] args)
{
string url_with_sasToken = "https://xxx.table.core.windows.net/ttt?sasToken";
MyClass entity = new MyClass
{
PartitionKey = "ivan2",
RowKey = "y1",
Name = "jack60",
TestId = Guid.NewGuid()
};
//here, I add the odata type for Guid type
string s = JsonConvert.SerializeObject(entity).Replace("}","");
s += ",\"TestId@odata.type\"" + ":" + "\"Edm.Guid\"}";
Console.WriteLine(s);
var content = new StringContent(s, Encoding.UTF8, "application/json");
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
var t2 = client.PostAsync(url_with_sasToken , content).GetAwaiter().GetResult();
Console.WriteLine(t2.StatusCode);
}
Console.WriteLine("completed---");
Console.ReadLine();
}
然后检查Azure门户: