|
1 | | -using Microsoft.EntityFrameworkCore; |
2 | | -using Microsoft.Extensions.DependencyInjection; |
| 1 | +namespace KingbaseES.BasicTest; |
3 | 2 |
|
4 | | -namespace KingbaseES.BasicTest |
| 3 | +internal static class Program |
5 | 4 | { |
6 | | - internal static class Program |
| 5 | + static void Main(string[] args) |
7 | 6 | { |
8 | | - static void Main(string[] args) |
9 | | - { |
10 | | - var services = new ServiceCollection(); |
| 7 | + var services = new ServiceCollection(); |
11 | 8 |
|
12 | | - services.AddDbContext<BlogContext>(options => |
13 | | - { |
14 | | - options.UseKdbndp(@"host=localhost;port=54321;database=test;user id=system;password=123456;"); |
15 | | - }); |
| 9 | + services.AddDbContext<BlogContext>(options => |
| 10 | + { |
| 11 | + options.UseKdbndp(@"host=localhost;port=54321;database=test;user id=system;password=123456;"); |
| 12 | + }); |
16 | 13 |
|
17 | | - var serviceProvider = services.BuildServiceProvider(); |
| 14 | + var serviceProvider = services.BuildServiceProvider(); |
18 | 15 |
|
19 | | - var context = serviceProvider.GetRequiredService<BlogContext>(); |
| 16 | + var context = serviceProvider.GetRequiredService<BlogContext>(); |
20 | 17 |
|
21 | | - context.Database.EnsureCreated(); |
| 18 | + context.Database.EnsureCreated(); |
22 | 19 |
|
23 | | - // get list |
24 | | - var blogs = context.Blogs.ToList(); |
25 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "当前 Blogs 表中条目数:" + blogs.Count); |
26 | | - if (blogs.Any()) |
27 | | - { |
28 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "当前 Blogs 表中条目数大于1,进行清理..."); |
29 | | - context.Blogs.RemoveRange(blogs); |
30 | | - context.SaveChanges(); |
31 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "清理完成!"); |
32 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "当前 Blogs 表中条目数:" + blogs.Count); |
33 | | - } |
34 | | - //add |
35 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "新增 Blog 实体 值为I love EFCore!"); |
36 | | - context.Add(new Blog() { Name = "I love EFCore!" }); |
37 | | - var result = context.SaveChanges(); |
38 | | - |
39 | | - //update |
40 | | - var first = context.Blogs.FirstOrDefault(); |
41 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "查询结果:" + first.Name); |
42 | | - |
43 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "变更 Blog 实体 值为I love EFCore too!"); |
44 | | - first.Name = "I love EFCore too!"; |
| 20 | + // get list |
| 21 | + var blogs = context.Blogs.ToList(); |
| 22 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "当前 Blogs 表中条目数:" + blogs.Count); |
| 23 | + if (blogs.Any()) |
| 24 | + { |
| 25 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "当前 Blogs 表中条目数大于1,进行清理..."); |
| 26 | + context.Blogs.RemoveRange(blogs); |
45 | 27 | context.SaveChanges(); |
46 | | - |
47 | | - // get |
48 | | - var testselect = context.Blogs.FirstOrDefault(); |
49 | | - Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "查询结果:" + testselect.Name); |
50 | | - Console.ReadKey(); |
| 28 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "清理完成!"); |
| 29 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "当前 Blogs 表中条目数:" + blogs.Count); |
51 | 30 | } |
| 31 | + //add |
| 32 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "新增 Blog 实体 值为I love EFCore!"); |
| 33 | + context.Add(new Blog() { Name = "I love EFCore!" }); |
| 34 | + var result = context.SaveChanges(); |
| 35 | + |
| 36 | + //update |
| 37 | + var first = context.Blogs.FirstOrDefault(); |
| 38 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "查询结果:" + first.Name); |
| 39 | + |
| 40 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "变更 Blog 实体 值为I love EFCore too!"); |
| 41 | + first.Name = "I love EFCore too!"; |
| 42 | + context.SaveChanges(); |
| 43 | + |
| 44 | + // get |
| 45 | + var testselect = context.Blogs.FirstOrDefault(); |
| 46 | + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "查询结果:" + testselect.Name); |
| 47 | + Console.ReadKey(); |
52 | 48 | } |
53 | 49 | } |
0 commit comments