在 C# 中填充 Azure Table 存储 Tables

Populate Azure Table Storage Tables in C#

我正在尝试找到一种使用 C# 一次性填充 Table Azure Table 存储的好方法。现在,我正在使用 CloudTable.CreateIfNotExistAsync() 创建启动时需要的表。我的意思是我可以检查我需要的初始条目是否已经在里面,如果没有创建它们,但也许有更聪明的方法来做到这一点。也许 C# SDK 有一些我还没有意识到的机制。

I mean I could check if the initial entries I need, are already inside and if not create them but maybe there is a more clever way to do it. Maybe the C# SDK has some mechanisms I didn't recognize yet.

对于您的场景,我建议对实体进行 InsertOrReplace 操作,如果实体不存在则创建实体,如果实体存在则替换实体。

var account = CloudStorageAccount.Parse("UseDevelopmentStorage=true");// new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);

var tableClient = account.CreateCloudTableClient();

var table = tableClient.GetTableReference("Customer");
table.CreateIfNotExists();

var entity = new DynamicTableEntity("partitionkey", "rowkey");
entity.Properties.Add("key", new EntityProperty("value"));

var operation = TableOperation.InsertOrReplace(entity);

var result = await table.ExecuteAsync(operation);

其他选项可能是尝试创建一个实体并捕获 StorageException 如果该实体已经存在,将抛出该实体。