分支自 SuZhouGuanHong/TaiYuanTaiZhong

PCS
dengjunjie
2023-12-13 113d1d4262d8f9e78a9d92123713c41669ad6c87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using FreeSql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.DBManager;
 
namespace WIDESEA_Core.FreeDB
{
    public class FreeDB
    {
        private static IFreeSql DB;
 
        public IFreeSql DataBase { get => DB; }
 
        public FreeDB()
        {
            if (DB == null)
            {
                string coon = DBServerProvider.GetConnectionString();
                DB = new FreeSql.FreeSqlBuilder()
                   .UseConnectionString(FreeSql.DataType.SqlServer, coon)
                   .Build();
            }
        }
 
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add<T>(T entity) where T : class
        {
            return DB.Insert<T>(entity).ExecuteAffrows();
        }
 
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityList"></param>
        /// <returns></returns>
        public int AddRange<T>(List<T> entityList) where T : class
        {
            return DB.Insert<T>(entityList).ExecuteAffrows();
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Remove<T>(T entity) where T : class
        {
            return DB.Delete<T>(entity).ExecuteAffrows();
        }
 
        /// <summary>
        /// 更新(拓展方法)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public IUpdate<T> Update<T>(T entity) where T : class
        {
            return DB.Update<T>(entity).SetSource(entity);
        }
 
        /// <summary>
        /// 更新(拓展方法)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IUpdate<T> Update<T>() where T : class
        {
            return DB.Update<T>();
        }
 
        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public ISelect<T> Select<T>() where T : class
        {
            return DB.Select<T>();
        }
 
        /// <summary>
        /// 事务(完整事务)
        /// </summary>
        /// <param name="action"></param>
        public void Transaction(Action action)
        {
            DB.Transaction(action);
        }
 
        /// <summary>
        /// 事务(上下文事务)
        /// </summary>
        /// <param name="action"></param>
        public void Context(Action<DbContext> action)
        {
            using (var db = DB.CreateDbContext())
            {
                action(db);
            }
        }
    }
}