使用 Azure Table API 的 .NET Core CRUD 操作

作者 : 慕源网 本文共3316个字,预计阅读时间需要9分钟 发布时间: 2021-10-20 共607人阅读

介绍

Azure Table 是一种高度可用的 NoSQL 服务,可帮助我们构建可扩展的应用程序。这使我们的开发过程更加容易。它允许我们在不提及表模式的情况下更改应用程序中的模型。

  • 键值对
  • 低延迟
  • 可扩展的应用程序和高可用性
  • 灵活的数据结构
  • 用于序列化数据的 JSON
  • 主要区域将具有读/写功能,但次要区域将是只读的

  • 创建 .NET Core 应用程序并安装 NuGet 包“Microsoft.Azure.Cosmos.Table”。然后,转到 此处 并导航到存储帐户并在“访问密钥”选项卡下获取连接密钥。
  • 为“CloudTableClient”创建一个对象,它允许我们对存储帐户内的表执行操作。
  • 创建一个自定义的表实体并继承“TableEntity”类,它带有很多重要的项目,如分区键、行键等。
  • 编写一个存储库方法来填充一个特定表下的所有值,对于这个演示,我们使用的是同步方法,但在实时我们应该使用 async 和 await 方法以获得更好的体验。
  • “TableQuery”将允许我们对特定表执行特定操作,如插入、删除、更新。
  • 创建控制器类和方法以从 Azure table  中提取记录。在“模型”文件夹下创建模型,代表我们想要在 UI 中显示的数据。分区键用于对表中的特定行进行分组。
  • azure table  API 中提供了多种过滤器,可以对表查询执行,即为二进制、布尔、日期等生成过滤条件。
  • 创建一个“TableOperation”来对特定的表执行插入、删除和更新,如下所示。
  • 创建控制器并查看页面以在 UI 上执行适当的操作。
  • 在这个演示中,我们使用了 MVC 脚手架技术来生成 UI
  • 按照上面的简单步骤从 Azure Table API 获取详细信息,如果您遇到任何问题,请告诉我。

Repository.CS

public class MyDemoRepository    
    {    
        private CloudTable mytable = null;    
        public MyDemoRepository()    
        {    
            var storageAccount = CloudStorageAccount.Parse("your azure storage key goes here..");    
            var cloudTableClient = storageAccount.CreateCloudTableClient();    
            mytable = cloudTableClient.GetTableReference("Customer");    
            mytable.CreateIfNotExists();    
        }    
    
        public IEnumerable<MyTableEntity> GetAll()    
        {    
            var query = new TableQuery<MyTableEntity>();    
            var entties = mytable.ExecuteQuery(query);    
            return entties;    
        }    
        public void CreateOrUpdate(MyTableEntity myTableOperation)    
        {    
            var operation = TableOperation.InsertOrReplace(myTableOperation);    
            mytable.Execute(operation);    
        }    
        public void Delete(MyTableEntity myTableOperation)    
        {    
            var operation = TableOperation.Delete(myTableOperation);    
            mytable.Execute(operation);    
        }    
        public MyTableEntity Get(string partitionKey, string RowId)    
        {    
            var operation = TableOperation.Retrieve<MyTableEntity>(partitionKey, RowId);    
            var result= mytable.Execute(operation);    
            return result.Result as MyTableEntity;    
        }    
    }    
    
    public class MyTableEntity : TableEntity    
    {    
        public string Name { get; set; }    
        public string Address { get; set; }    
    }  

CustomerController.cs

public class CustomerController : Controller    
    {    
        //Get the data from Azure table    
        public IActionResult GetAll()    
        {    
            var repositoty = new MyDemoRepository();    
            var entities = repositoty.GetAll();    
            var model = entities.Select(x => new CustomerModel    
            {    
                Group = x.PartitionKey,    
                ID = x.RowKey,    
                Name = x.Name,    
                Addres = x.Address    
            });    
            return View(model);    
        }    
        public IActionResult Create()    
        {    
            return View();    
        }    
        public IActionResult Edit(string group, string id)    
        {    
            var repositoty = new MyDemoRepository();    
            var item = repositoty.Get(group, id);    
            return View("Edit", new CustomerModel    
            {    
                Group = item.PartitionKey,    
                ID = item.RowKey,    
                Name = item.Name,    
                Addres = item.Address    
            });    
               
        }    
        public IActionResult ConfirmDelete(string group, string id)    
        {    
            var repositoty = new MyDemoRepository();    
            var item = repositoty.Get(group, id);    
            return View("Delete", new CustomerModel    
            {    
                Group = item.PartitionKey,    
                ID = item.RowKey,    
                Name = item.Name,    
                Addres = item.Address    
            });    
    
        }    
    
        [HttpPost]    
        public IActionResult Delete(string group, string id)    
        {    
            var repositoty = new MyDemoRepository();    
            var item = repositoty.Get(group, id);    
            repositoty.Delete(item);    
            return RedirectToAction("GetAll");    
    
        }    
        [HttpPost]    
        public IActionResult Create(CustomerModel customerModel)    
        {    
            var repositoty = new MyDemoRepository();    
            repositoty.CreateOrUpdate(new MyTableEntity    
            {    
                PartitionKey = customerModel.Group,    
                RowKey = Guid.NewGuid().ToString(),    
                Name = customerModel.Name,    
                Address = customerModel.Addres    
    
            });    
            return RedirectToAction("GetAll");    
        }    
    
        [HttpPost]    
        public IActionResult Edit(CustomerModel customerModel)    
        {    
            var repositoty = new MyDemoRepository();    
            repositoty.CreateOrUpdate(new MyTableEntity    
            {                   
                Name = customerModel.Name,    
                Address = customerModel.Addres    
    
            });    
            return RedirectToAction("GetAll");    
        }    
    }   

概括

在本节中,我们在 .NET Core 中创建了一个简单的 CRUD 应用程序,并从 Azure Table API 服务中检索了数据。

慕源网 » 使用 Azure Table API 的 .NET Core CRUD 操作

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录