【发布时间】:2021-06-29 04:08:05
【问题描述】:
我正在尝试创建一个通用插入方法,我从抽象 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, "TE3", "test", "nav__test")
);
Console.WriteLine($"{result.response} __ {result.success}");
但在我的 Azure 表中,字段 Abbrev、Name 和 NavName 设置为 null。
这是因为我在插入函数中将TableEntity 声明为类型而不是STCompany,还是我做错了什么?
【问题讨论】:
标签: c# azure-storage azure-table-storage