我是天蓝色表的新手,我只想创建表,插入项目并将其取回。
我有实体类:
public class UserEntity : TableEntity
{
public string ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public DateTime RegistrationTime { get; set; }
public string Password { get; set; }
public int Country { get; set; }
public UserEntity() { }
public UserEntity(MyUser user)
{
this.ID = user.ID.ToString();
this.Name = user.Name;
this.Surname = user.Surname;
this.Email = user.Email;
this.RegistrationTime = user.RegistrationTime;
this.Password = user.Password.ToString();
this.Country = user.Country;
this.PartitionKey = this.Country.ToString();
this.RowKey = this.Email;
}
}
创建表我使用函数:
public static void CreateTable()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.Azure_Storage_ConnectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TBUsers");
table.CreateIfNotExists();
}
插入新实体:
public void SaveAsNew()
{
UserEntity user = new UserEntity(this);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.Azure_Storage_ConnectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TBUsers");
TableOperation insertOperation = TableOperation.Insert(user);
var res = table.Execute(insertOperation);
}
创建和插入都可以正常工作(至少不要抛出异常并且 table.Execute 返回正确的实体)
但我有问题,我想拿到我的物品。代码看起来像这样
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.Azure_Storage_ConnectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TBUsers");
TableOperation retrieveOperation = TableOperation.Retrieve<UserEntity>(pkey, rkey);
TableResult retrievedResult = table.Execute(retrieveOperation);
retrievedResult.Result - 是一个奇怪的 UserEntity 对象。它的 PartitionKey 和 RowKey 很好,但其他属性为 null 或 0(默认值)。
难道我做错了什么?