Azure Cosmos DB (二) SQL API 操作
一,引言
还记得国庆期间,我们学习了一下关于Azure Cosmos DB 的一些基础知识以及Azure Cosmos DB 的几种支持数据库类型。今天就开始分享一些实战操作,如何通过Azure Portal 创建 Cosmos DB,以及在实际项目中如何通过代码操作数据库。
今天要演示的是 Core(SQL) 核心,SQL API 是具有丰富的SQL查询功能的无架构 JSON 数据库引擎,它是以 json 形式的文档数据库。同时微软提供了对应的 EF 的操作包 ----- Microsoft.EntityFrameworkCore.Cosmos,使得我们很轻松的使用原有EF操作数据库的模式进行操作 Cosmos DB。
注意这里使用的 Microsoft.EntityFrameworkCore.Cosmos 仅适用于 Azure Cosmos DB 的 SQL API
--------------------我是分割线--------------------
1,Azure Cosmos DB (一) 入门介绍
2,Azure Cosmos DB (二) SQL API 操作
二,正文
1, 创建Azure Cosmos DB
在Azure portal 中点击 “+ 创建资源”,在搜索框中输入 “Azure Cosmos DB” 进行搜索

点击 “Create” ,进行开始创建

Resource Group:"Web_Test_DB_RG"
Account Name:"cnbateblogwebcosmosdb"
API 选择:Core(SQL)
Capacity mode 选择默认 :“Provisioned throughtput”(预配的吞吐量)
其他都选择默认。点击 “Review + create” 进行创建前的预校验。
使用Azure Cosmos DB 免费层时,将在账户中免费获得 400 RU/秒和 5GB 的存储。每个订阅最多可对一个账户启用免费层。预计每个账户每月有24美元的折扣。

可以看到验证提示 “Validation Sucess”,点击 ”Create“ 进行创建

等待几分钟后,我们可以在 "Web_Test_DB_RG" 中找到刚刚创建好的叫 ”cnbateblogwebcosmosdb“ 的 Cosmos DB 了

点击进去,找到 Data Explorer ,点击 ”New Database“ 新建数据库

或者点击 ”New Container“ 旁边的小箭头,进行下拉选择创建新的数据库。

Database id:”CNBATEBLOGWEB“
点击 “OK” ,进行创建

可以看到 “CNBATEBLOGWEB” 的 Database 已创建好了。

2,项目添加对Cosmos DB 的依赖包
2.1,安装 “Microsoft.EntityFrameworkCore”,“Microsoft.EntityFrameworkCore.Cosmos”

使用程序包管理控制台进行安装
Install-Package Microsoft.EntityFrameworkCore -Version 3.1.8
Install-Package Microsoft.EntityFrameworkCore.Cosmos -Version 3.1.8
2.2,要创建所需的容器并插入种子数据
配置环境,此时需要cosmosDB 的 Endpoint,Key,DatabaseName
图中对应的 URL=》Endpoint,PRIMARK KEY=》Key,
上面步骤中创建的叫 “CNBATEBLOGWEB” 的我们需要的DatabaseName

整理好刚才需要的三个参数,添加到项目的配置文件中 也就是 appsettings.json 文件中

创建UserModel 实体,UserContext,重写 OnConfiguring 方法,调用UseCosmos 方法


重写 “OnModelCreating” 创建数据数据映射关系

”ConfigrueServices“ 中添加对 “UserContext” 的依赖注入

生产种子数据,先将已有的数据库进行删除操作,再进行添加操作以及生成种子数据

完整代码

1 public class UserContext : DbContext
2 {
3 public UserContext(DbContextOptions<UserContext> options) : base(options)
4 {
5
6 }
7
8 public DbSet<UserModel> Users { get; set; }
9
10 /// <summary>
11 /// 重写连接数据库
12 /// </summary>
13 /// <param name="optionsBuilder"></param>
14 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
15 {
16 // 从 appsetting.json 中获取配置信息
17 var config = new ConfigurationBuilder()
18 .SetBasePath(Directory.GetCurrentDirectory())
19 .AddJsonFile("appsettings.json")
20 .Build();
21
22 // 定义要使用的数据库
23 optionsBuilder.UseCosmos(Appsettings.app("CosmosDB", "Endpoint"), Appsettings.app("CosmosDB", "Key"), Appsettings.app("CosmosDB", "DataBase"));
24 }
25
26 protected override void OnModelCreating(ModelBuilder modelBuilder)
27 {
28 //针对于HasData限制(即使主键是由数据库生成的自动增长),也需要指定主键
29
30 //调用EnsureCreated方法只针对与添加数据有效,但是数据库如果有数据的话,
31 //也就是对数据更改将无效
32 Console.WriteLine("**********UserModel表开始初始化数据**********");
33 #region 数据库数据映射
34 modelBuilder.Entity<UserModel>().HasData(
35 //new Blog{Id=1,Name="DDD领域驱动模型"},
36 new UserModel { Id = 1, Name = "EntityFramework Core 3.1.1" },
37 new UserModel { Id = 2, Name = "EntityFramework Core 3.1.6" });
38
39 #endregion
40
41
42 }
43
44 }
UserContext.cs

1 public class UserModel
2 {
3 public int Id { get; set; }
4
5 public string Name { get; set; }
6
7 public int Age { get; set; }
8
9 public string Address { get; set; }
10
11 public string Remark { get; set; }
12 }
UserModel.cs

1 /// <summary>
2 /// appsettings.json操作类
3 /// </summary>
4 public class Appsettings
5 {
6 static IConfiguration Configuration { get; set; }
7 static string contentPath { get; set; }
8
9 public Appsettings(string contentPath)
10 {
11 string Path = "appsettings.json";
12
13
14 //如果你把配置文件 是 根据环境变量来分开了,可以这样写
15 //string Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
16
17 //var contentPath = env.ContentRootPath;
18 Configuration = new ConfigurationBuilder()
19 .SetBasePath(contentPath)
20 .Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })//这样的话,可以直接读目录里的json文件,而不是 bin 文件夹下的,所以不用修改复制属性
21 .Build();
22 }
23
24 /// <summary>
25 /// 封装要操作的字符
26 /// </summary>
27 /// <param name="sections"></param>
28 /// <returns></returns>
29 public static string app(params string[] sections)
30 {
31 try
32 {
33 var val = string.Empty;
34 for (int i = 0; i < sections.Length; i++)
35 {
36 val += sections[i] + ":";
37 }
38
39 return Configuration[val.TrimEnd(':')];
40 }
41 catch (Exception)
42 {
43 return "";
44 }
45 }
46 }
Appsettings.json

1 public class Program
2 {
3 public static void Main(string[] args)
4 {
5 var host = CreateHostBuilder(args).Build();
6 // 创建可用于解析作用域服务的新 Microsoft.Extensions.DependencyInjection.IServiceScope。
7 using (var scope = host.Services.CreateScope())
8 {
9 var services = scope.ServiceProvider;
10 var loggerFactory = services.GetRequiredService<ILoggerFactory>();
11 var env = services.GetRequiredService<IWebHostEnvironment>();
12 if (env.IsDevelopment())
13 {
14 try
15 {
16 // 从 system.IServicec提供程序获取 T 类型的服务。
17 var context = services.GetRequiredService<UserContext>();
18
19 context.Database.EnsureDeleted();
20 Console.WriteLine("**********开始初始化数据**********");
21 context.Database.EnsureCreated();
22
23 }
24 catch (Exception e)
25 {
26 var logger = loggerFactory.CreateLogger<Program>();
27 logger.LogError(e, "Error occured seeding the Database.");
28 }
29 }
30 }
31
32 // 运行 web 应用程序并阻止调用线程, 直到主机关闭。
33 // 创建完 WebHost 之后,便调用它的 Run 方法,而 Run 方法会去调用 WebHost 的 StartAsync 方法
34 // 将Initialize方法创建的Application管道传入以供处理消息
35 // 执行HostedServiceExecutor.StartAsync方法
36
37 host.Run();
38 }
39
40 public static IHostBuilder CreateHostBuilder(string[] args) =>
41 Host.CreateDefaultBuilder(args)
42 .ConfigureWebHostDefaults(webBuilder =>
43 {
44 webBuilder
45 .UseUrls("http://*:9001")
46 .UseStartup<Startup>();
47 });
48 }
Program.cs

1 public class Startup
2 {
3 public Startup(IConfiguration configuration, IWebHostEnvironment env)
4 {
5 Env = env;
6 Configuration = configuration;
7 }
8
9 public IConfiguration Configuration { get; }
10
11 public IWebHostEnvironment Env { get; }
12
13 // This method gets called by the runtime. Use this method to add services to the container.
14 public void ConfigureServices(IServiceCollection services)
15 {
16 services.AddSingleton(new Appsettings(Env.ContentRootPath));
17
18 services.AddDbContext<UserContext>(options => options.UseCosmos(Appsettings.app("CosmosDB", "Endpoint"), Appsettings.app("CosmosDB", "Key"), Appsettings.app("CosmosDB", "DataBase")));
19
20 services.AddControllersWithViews();
21 }
22
23 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
24 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
25 {
26 if (env.IsDevelopment())
27 {
28 app.UseDeveloperExceptionPage();
29 }
30 else
31 {
32 app.UseExceptionHandler("/Home/Error");
33 }
34 app.UseStaticFiles();
35
36 app.UseRouting();
37
38 app.UseAuthorization();
39
40 app.UseEndpoints(endpoints =>
41 {
42 endpoints.MapControllerRoute(
43 name: "default",
44 pattern: "{controller=Home}/{action=Index}/{id?}");
45 });
46 }
47 }
Startup.cs
3,运行项目,查看测试结果
我们可以看到UserModel 表已初始化数据完成

我们转到Azure Portal 上查看 "CNBATEBLOGWEB" 数据库下多了叫 “UserContext” 的 Container 这里的可以理解成存放表的容器

同时,我们可以看到 ”UserContext“ 下的 Item 多了两条 Usermodel 数据,我们分别进行查看,可以看到这两条数据就是刚刚生成的种子数据


ok,今天的分享就到此,撒花是
-------------------我是分割线-------------------
本来我想尝试直接可以通过VS 的 Cloud Explorer 进行查看 cosmosDB 中的数据,但是无奈每当点击 Container 下的更多的时候,VS 就进行报错

有知道怎么解决这个问题的朋友,还望告知一下。
三,结尾
今天我们先操作这么多,简单的使用 EF 对 Azure Cosmos DB 进程操作,生产种子数据,以及如何查看生成好的种子数据,下一篇继续讲解使用 EF 来操作操作Azure CosmosDB,并且对其中的数据库中的数据如何做增加,删除,修改,查询等操作。
github:https://github.com/yunqian44/Azure.CosmosDB.git
作者:Allen
版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。
Azure Cosmos DB (二) SQL API 操作的更多相关文章
- Azure Cosmos DB (三) EF Core 操作CURD
一,引言 接着上一篇使用 EF Core 操作 Azure CosmosDB 生成种子数据,今天我们完成通过 EF Core 实现CRUD一系列功能.EF Core 3.0 提供了CosmosDB 数 ...
- Azure Cosmos DB (四) 使用EF的SQL API 异地冗余
一,引言 上一篇文章中,我们介绍到使用了EF Core 与Cosmos DB SQL API 进行结合开发.同时,大家在开发过程中一定要记得EF Core 不支持Cosmos DB 的迁移.今天我们启 ...
- Azure Cosmos DB (五) .Net Core 控制台应用
一,引言 之前在讲Azure CosmosDB Core(SQL)核心的时候,使用了EF Core 的方式,引用了 "Microsoft.EntityFrameworkCore.Cosmos ...
- Azure Cosmos DB (一) 入门介绍
一,引言 今天是国庆.中秋双节房价的第三天,今天抽时间分享一篇关于使用Azure 提供的一项NoSql 服务-----Azure Cosmos DB.就有人问了,我听说过 MongoDB.Redis ...
- NCF 的Azure Cosmos DB 演示案例
简介 NCF想必看过我之前发的NCF的文章的同学们都已经很熟悉了 今天我们要来聊一聊的是NCF遇到Azure Cosmos DB后会碰撞出什么样的火花,让我们一起往下看 我们先来说说什么是Azure ...
- Azure Cosmos DB 使用费用参考
之前在学习Cosmos DB 中SQL API(DocumentDB) 的时候,也就是之前做的一些笔记,看到有使用费用的一些介绍,就有兴趣的去了解了下,做了一下简单的总结. 想了解更多或是购买使用的还 ...
- Azure Cosmos DB介绍及演示
Azure Cosmos DB 是 Microsoft 提供的全球分布式多模型数据库服务.Cosmos DB是一种NoSql数据库,但是它兼容多种API.它支持SQL, MongoDB.Cassand ...
- Azure CosmosDB (10) Azure Cosmos DB体系结构
<Windows Azure Platform 系列文章目录> Azure Cosmos DB的体系结构分为以下几个部分: 1.Database Accounts Database Acc ...
- azure cosmos db (mongo DB)
使用.net mongo的操作类操作azure(微软云)cosmosdb时,发现在做delete的操作的时候可以传一个文档对象,但是最后这个文档会解析成具体的sql语句,而当文档特别大时这样就出先了转 ...
随机推荐
- [BUUOJ记录] [GXYCTF2019]BabyUpload
CTF三大骗局:Baby Easy Funny,本题主要考察.htaccess文件解析文件.文件类型检测绕过 打开题目给了一个上传点,上传一个php文件看看过滤规则 “后缀名不能有ph”直接禁掉了所有 ...
- JDK15就要来了,你却还不知道JDK8的新特性!
微信搜「烟雨星空」,白嫖更多好文. 现在 Oracle 官方每隔半年就会出一个 JDK 新版本.按时间来算的话,这个月就要出 JDK15 了.然而,大部分公司还是在使用 JDK7 和 8 . 之前去我 ...
- 非IT行业大企程序员讲述MIS系统开发案例
雪莉叹了一口气,调整了一下被汗水濡湿的刘海,然后向后靠在办公椅中,伸手在电脑键盘上输入了一些内容, 最后拿起印刷着房地产广告的扇子,边扇风边等待着. 她的工位在办公室的东侧角落,侧靠着窗.此时 ...
- 转载: Nginx 通览
转载地址:https://developer.51cto.com/art/201912/608365.htm Nginx 简介 Nginx 是一个免费.开源.高性能.轻量级的 HTTP 和反向代理服务 ...
- Ubuntu18.04
起因 晚上过来加班···· 问题 物理机安装ubuntu时候遇到报错信息 boot failed: please change disks and press a key to continue ht ...
- linux基础命令一、
命令格式: 命令 -选项 参数 uname -r 查看内核版本 uname -m 查看系统版本 alias 别名. 举例: alias grep ='grep --color=auto' ...
- 大写的服,看完这篇你还不懂RocketMQ算我输
目录 RocketMQ介绍 RocketMQ概念 为什么要用RocketMQ? 异步解耦 削峰填谷 分布式事务最终一致性 数据分发 RocketMQ架构 RocketMQ消息类型 普通消息 顺序消息 ...
- [LeetCode]69. x 的平方根(数学,二分)
题目 https://leetcode-cn.com/problems/sqrtx 题解 方法一:牛顿迭代法 按点斜式求出直线方程(即过点Xn,f(Xn)),然后求出直线与x轴交点,即为Xn+1: 求 ...
- python里面的project、package、module分别是什么
2020/5/25 1.project(项目) project 即项目,是一个很大的文件夹,里面有好多的 .py 文件. 在Spyder 中点击菜单栏 projects -----> new ...
- 修改ubuntu默认源
修改文件 桌面版Ubuntu默认非root账户 可以先在 home/user/下新建一个文件source.list-new 将其写为 deb http://mirrors.aliyun.com/ubu ...