|
1 | 1 | using Mysql.Model; |
| 2 | +using Mysql.Services; |
2 | 3 | using MyStaging.Common; |
3 | 4 | using MyStaging.Interface; |
4 | 5 | using MyStaging.Metadata; |
5 | 6 | using MyStaging.MySql.Generals; |
6 | 7 | using Newtonsoft.Json.Linq; |
7 | 8 | using System; |
| 9 | +using System.Collections.Generic; |
8 | 10 |
|
9 | 11 | namespace Mysql |
10 | 12 | { |
11 | 13 | public class Program |
12 | 14 | { |
| 15 | + private static MysqlDbContext dbContext; |
13 | 16 | static void Main(string[] args) |
14 | 17 | { |
15 | | - //IGeneralFactory factory = new GeneralFactory(); |
16 | | - //factory.CodeFirst(new MyStaging.Metadata.ProjectConfig() |
17 | | - //{ |
18 | | - // ConnectionString = "server=127.0.0.1;user id=root;password=root;database=mystaging", |
19 | | - // Mode = MyStaging.Metadata.GeneralMode.Db, |
20 | | - // OutputDir = @"D:\MyGitHub\mystaging\examples\Mysql\Models", |
21 | | - // ProjectName = "Mysql", |
22 | | - // Provider = "MySql" |
23 | | - //}); |
| 18 | + var options = new StagingOptions("MySql", "server=127.0.0.1;user id=root;password=root;"); |
| 19 | + dbContext = new MysqlDbContext(options); |
24 | 20 |
|
25 | | - var options = new MyStaging.Metadata.StagingOptions("MySql", "server=127.0.0.1;user id=root;password=root;"); |
26 | | - var context = new MysqlDbContext(options); |
| 21 | + AddUpdateDelete(); |
| 22 | + Query(); |
| 23 | + Transaction(); |
| 24 | + Console.WriteLine("success....."); |
| 25 | + Console.ReadKey(); |
| 26 | + } |
27 | 27 |
|
| 28 | + static void Transaction() |
| 29 | + { |
28 | 30 | var customer = new Customer { Name = "好久不见" }; |
29 | 31 |
|
30 | 32 | try |
31 | 33 | { |
32 | 34 | // 测试事务 |
33 | | - context.BeginTransaction(); |
34 | | - context.Customer.Insert.Add(customer); |
35 | | - context.CommitTransaction(); |
| 35 | + dbContext.BeginTransaction(); |
| 36 | + dbContext.Customer.Insert.Add(customer); |
| 37 | + List<Customer> li = new List<Customer>(); |
| 38 | + li.Add(new Customer { Name = "test" }); |
| 39 | + dbContext.Customer.Insert.AddRange(li).SaveChange(); |
| 40 | + dbContext.Customer.Update.SetValue(a => a.Name, "12345").Where(f => f.Id == customer.Id).SaveChange(); |
| 41 | + dbContext.CommitTransaction(); |
36 | 42 |
|
37 | | - var nc = context.Customer.Select.Where(f => f.Id == customer.Id).ToOne(); |
| 43 | + ArticleService articleService = new ArticleService(dbContext); |
| 44 | + var art = articleService.Detail(29); |
| 45 | + art = articleService.Update(art.id, "修改了标题", art.content); |
| 46 | + bool success = articleService.Delete(art.id); |
38 | 47 | } |
39 | 48 | catch (Exception e) |
40 | 49 | { |
41 | 50 | Console.WriteLine(e.Message); |
42 | 51 | } |
43 | | - //// 单个查询 |
44 | | - //var article = context.Customer.Select.Where(f => f.Id == 2 && f.Name == "Ron").ToOne(); |
45 | | - //// 列表查询,排序、分页、分组 |
46 | | - //var articles = context.Customer.Select.OrderBy(f => f.Id).Page(1, 10).GroupBy("Name").ToList(); |
47 | | - //// 表连接查询 |
48 | | - //var article = context.Article.Select.InnerJoin<Customer>("b", (a, b) => a.userid == b.Id).Where<Customer>(f => f.Id == 2).ToOne(); |
49 | | - //// 首字段查询,ToScalar 参数可以传递 Sql 参数,比如 SUM(x) |
50 | | - //var id = context.Customer.Select.Where(f => f.Id == 2 && f.Name == "Ron").ToScalar<int>("Id"); |
51 | | - var a3 = context.Article.Update.SetValue(f => f.content, "未来已来,从这里开始").Where(f => f.id == 1).SaveChange(); |
52 | | - var article = new Article() |
| 52 | + } |
| 53 | + |
| 54 | + static void Query() |
| 55 | + { |
| 56 | + // 单个查询 |
| 57 | + var article = dbContext.Customer.Select.Where(f => f.Id == 2 && f.Name == "Ron").ToOne(); |
| 58 | + // 列表查询,排序、分页、分组 |
| 59 | + var articles = dbContext.Customer.Select.OrderBy(f => f.Name).Page(1, 10).GroupBy("Id,Name").ToList<(int id, string name)>("Id,Name"); |
| 60 | + // 表连接查询 |
| 61 | + var ac = dbContext.Article.Select.InnerJoin<Customer>("b", (a, b) => a.userid == b.Id).Where<Customer>(f => f.Id == 2).ToOne(); |
| 62 | + // 首字段查询,ToScalar 参数可以传递 Sql 参数,比如 SUM(x) |
| 63 | + var id = dbContext.Customer.Select.Where(f => f.Id == 2 && f.Name == "Ron").ToScalar<int>("Id"); |
| 64 | + } |
| 65 | + |
| 66 | + static void AddUpdateDelete() |
| 67 | + { |
| 68 | + var art = new Article() |
53 | 69 | { |
54 | 70 | content = "你是谁?你从哪里来?要到哪里去?", |
55 | 71 | createtime = DateTime.Now, |
56 | | - userid = customer.Id, |
| 72 | + userid = 43, |
57 | 73 | IP = "127.0.0.1", |
58 | 74 | State = true, |
59 | 75 | title = "振聋发聩的人生三问" |
60 | 76 | }; |
61 | 77 |
|
62 | | - var list = new System.Collections.Generic.List<Article>(); |
| 78 | + var articles = new List<Article>(); |
63 | 79 | for (int i = 0; i < 10; i++) |
64 | 80 | { |
65 | | - list.Add(article); |
| 81 | + articles.Add(art); |
66 | 82 | } |
67 | | - var a2 = context.Article.Insert.Add(article); |
68 | | - var affrows = context.Article.Insert.AddRange(list).SaveChange(); |
69 | | - Console.WriteLine(affrows); |
70 | | - // context.Article.Delete.Where(f => f.id == a2.id).SaveChange(); |
| 83 | + var a2 = dbContext.Article.Insert.Add(art); |
| 84 | + var affrows = dbContext.Article.Insert.AddRange(articles).SaveChange(); |
71 | 85 |
|
72 | | - // Console.WriteLine(a2.id); |
73 | | - |
74 | | - Console.WriteLine("success....."); |
75 | | - Console.ReadKey(); |
| 86 | + var a3 = dbContext.Article.Update.SetValue(f => f.content, "未来已来,从这里开始").Where(f => f.id == 1).SaveChange(); |
| 87 | + var a4 = dbContext.Article.Select.OrderByDescing(f => f.createtime).ToOne(); |
| 88 | + dbContext.Article.Delete.Where(f => f.id == a4.id).SaveChange(); |
76 | 89 | } |
77 | 90 | } |
78 | 91 | } |
0 commit comments