如何解决azure 本地存储表自定义字段为空
我正在尝试创建一个通用的插入方法,我从抽象的 TableEntity 类生成一个类。
class STCompany : TableEntity {
string Abbrev;
string Name;
string NavName;
public STCompany(
string partitionKey,string rowKey,DateTime timeStamp,string abbrev,string name,string navName
)
{
PartitionKey = partitionKey;
RowKey = rowKey;
Timestamp = timeStamp;
Abbrev = abbrev;
Name = name;
NavName = navName;
}
}
尝试在这个静态类中创建一个通用的插入方法(参见代码块底部的插入任务)
public static class lclStorage
{
private static string paramsConstring = __paramsConstring;
private static CloudStorageAccount storageAcc;
private static CloudTableClient tblclient;
private static CloudTable get_params_table (string table_name){
if(storageAcc == null) storageAcc = CloudStorageAccount.Parse(paramsConstring);
if(tblclient == null) tblclient = storageAcc.CreateCloudTableClient(new TableClientConfiguration());
return tblclient.GetTableReference(table_name);
}
public static class cloudTables{
public static CloudTable AccessUseRSShareholders = get_params_table("AccessUseRSShareholders");
public static CloudTable CompanyPropertyMapping = get_params_table("CompanyPropertyMapping");
public static CloudTable STCompanies = get_params_table("STCompanies");
public static CloudTable STdepartments = get_params_table("STdepartments");
}
public static async Task<(int statusCode,string response,bool success)> Insert(CloudTable p_tbl,TableEntity te)
{
TableOperation insertOperation = TableOperation.InsertOrMerge(te);
TableResult result = await p_tbl.ExecuteAsync(insertOperation);
Console.WriteLine("Record Added");
return (
result.HttpStatusCode,(result.HttpStatusCode == 204) ? "Record added" : "Failed to add record",result.HttpStatusCode == 204);
}
}
然后在我的主程序中运行插入
var result = await lclStorage.Insert(
lclStorage.cloudTables.STCompanies,new STCompany( "STCompanies","TE3",DateTime.Now,"test","nav__test")
);
Console.WriteLine($"{result.response} __ {result.success}");
在 TableResult result
中,字段显示正确
但在我的 Azure 表中,字段 Abbrev、Name 和 NavName 设置为 null。
这是因为我在插入函数中将 TableEntity
声明为类型而不是 STCompany
还是我做错了什么?
解决方法
所以 TableEntity 类中的变量 Abbrev,Name,NavName
必须是公共的。
class STCompany : TableEntity {
public string Abbrev { get; set; }
public string Name { get; set; }
public string NavName { get; set; }
public STCompany(
string partitionKey,string rowKey,string abbrev,string name,string navName
)
{
PartitionKey = partitionKey;
RowKey = rowKey;
Abbrev = abbrev;
Name = name;
NavName = navName;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。