Writing DynamicTableEntity to Azure Storage Table
There are ample of samples available to show how to insert an object/entity to Azure Storage Table. However, all the samples inherit from TableEntity
This sample shows how to insert custom entities to table when we don’t have a class that inherits from TableEntity
.
void Main() { var account = ""; var key = ""; var tableName = ""; var storageAccount = GetStorageAccount(account, key); var cloudTableClient = storageAccount.CreateCloudTableClient(); var table = cloudTableClient.GetTableReference(tableName); var partitionKey = "pk"; var rowKey = "rk"; //create the entity var entity = new DynamicTableEntity(partitionKey, rowKey, "*", new Dictionary<string,EntityProperty>{ {"Prop1", new EntityProperty("stringVal")}, {"Prop2", new EntityProperty(DateTimeOffset.UtcNow)}, }); //save the entity table.Execute(TableOperation.InsertOrReplace(entity)); //retrieve the entity table.Execute(TableOperation.Retrieve(partitionKey,rowKey)).Result.Dump(); } static CloudStorageAccount GetStorageAccount(string accountName, string key, bool useHttps = true) { var storageCredentials = new StorageCredentials(accountName, key); var storageAccount = new CloudStorageAccount(storageCredentials, useHttps: useHttps); return storageAccount; }
This code makes use of DynamicTableEntity
which can take properties and values as IDictionary
.
🐳 作者:dupeng0811 📢 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接 💬 留言:同时 , 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |
|