ORM.DataAccess(一)

 

一.通过nuget安装

 

搜索 ORM.DataAccess 安装,建议安装最新版本

需要支持不同数据库,安装不同的ORM.DataAccess对应数据库版本,目前支持Oracle,MSSQLServer,MySQL,PostgreSQL,Doris

framework版本,支持4.7及4.7以上,.netcore支持.net6及以上

二.配置

1.framework,打开web.config或app.config,在 configuration节点下添加
<configSections>
    <section name="RB.DataAccess" type="RB.DataAccess.Configuration.DataAccessConfigSection, RB.DataAccess" />
</configSections>

<!-- defaultContextName:默认数据库上下文名称,必填  -->
<!-- dbLogType:记录sql执行日志方式,目前支持DB(数据库)和Text(文本),选填 -->
<!-- logInterval:记录日志频率,单位秒,选填 -->
<!-- limitDuration:记录日志的最小执行时间,单位秒,选填 -->
<!-- context name:数据库上下问名称,全局唯一,必填 -->
<!-- context enable:是否启用,默认true,选填 -->
<!-- context type:数据库类型,默认Oracle,支持Oracle,MySQL,SqlServer,PostgreSql,选填 -->
<!-- ontext privateKeyPath:非对称加密密钥目录,只有连接字符串用非对称加密才有效,选填 -->
<!-- context connectionString:链接字符串,根据各数据库方式填写完整字符串,支持对称加密和非对称加密,必填 -->

<RB.DataAccess>
    <contexts defaultContextName="DCS" dbLogType="DB" logInterval="30" limitDuration="2">
         <context name="MeetingDB" enable="false" type="MySql" privateKeyPath="C:\DCSConfig\DCS" connectionString="xxxx" />
    </contexts>
</RB.DataAccess>
2.netcore配置,新增一个dbs.json文件
[
  {
    "contextName": "ICS",
    "dbType": "MySQL",
    "connectionString": "xxx",
    "enable": false,
    "desc": "ICS正式区"
  },
  {
    "contextName": "ICS",
    "dbType": "MySQL",
    "connectionString": "xxx",
    "enable": true,
    "desc": "ICS测试区"
  }
]
Ps:具体说明参考上面注释,默认第一个启用的是默认数据上下文

三.项目初始化

.netframework,在Application_Start或Main函数中执行,整个项目启动只需要初始化一次即可
//初始化里面的回调函数,可以为空,ModifyBy/ModifyByName用来自动记录修改人信息
DataContextFactory.InitRB(() =>
{
    return new RBLogDataModel()
    {
        ModifyBy = "工号",
        ModifyByName = "姓名"
    };
});
.netcore中,在Main中执行
//初始化数据库配置文件,第1个参数为配置文件相对路劲,isFormal:配置文件第一条是否是正式区
DataContextFactory.InitRB("/Configs/dbs.json", () =>
{
    return new RBLogDataModel()
    {
        ModifyBy = "工号",
        ModifyByName = "姓名"
    };
}, true);

四.基本使用

1.这里定义3个表对象,学生(Student),课程(Course),学生选的课程(StudentCours)

 1 /// <summary>
 2 /// 学生表
 3 /// </summary>
 4 public class Student
 5 {
 6     /// <summary>
 7     /// 学好
 8     /// </summary>
 9     public string StuCode { set; get; }
10 
11     /// <summary>
12     /// 姓名
13     /// </summary>
14     public string StuName { set; get; }
15 
16     /// <summary>
17     /// 身高
18     /// </summary>
19     public float? Height { set; get; }
20 
21     /// <summary>
22     /// 性别
23     /// </summary>
24     public SexEnum Sex { set; get; }
25 
26     /// <summary>
27     /// 出生日期
28     /// </summary>
29     public DateTime? Birthday { set; get; }
30 }
31 
32 /// <summary>
33 /// 性别枚举
34 /// </summary>
35 public enum SexEnum
36 {
37     /// <summary>
38     ///39     /// </summary>
40     Male = 1,
41 
42     /// <summary>
43     ///44     /// </summary>
45     Female = 2
46 }
47 
48 /// <summary>
49 /// 课程表
50 /// </summary>
51 public class Course
52 {
53     /// <summary>
54     /// 课程编号
55     /// </summary>
56     public string CourseCode { set; get; }
57 
58     /// <summary>
59     /// 课程名称
60     /// </summary>
61     public string CourseName { set; get; }
62 }
63 
64 /// <summary>
65 /// 学生与课程关联表
66 /// </summary>
67 public class StudentCourse
68 {
69     /// <summary>
70     /// 学生学号
71     /// </summary>
72     public string StuCode { set; get; }
73 
74     /// <summary>
75     /// 课程编号
76     /// </summary>
77     public string CourseCode { set; get; }
78 }
View Code

2.定义3个关联数据业务集

 1 /// <summary>
 2 /// 学生表集
 3 /// </summary>
 4 public class StudentSet : RecordSet<Student>
 5 {
 6 }
 7 
 8 /// <summary>
 9 /// 课程集合
10 /// </summary>
11 public class CourseSet : RecordSet<Course>
12 {
13 }
14 
15 /// <summary>
16 /// 学生课程表集
17 /// </summary>
18 public class StudentCourseSet : RecordSet<StudentCourse>
19 {
20 }
View Code

3.基本操作(增删改,单表查询)

 1 //所有操作,自动commit提交
 2 //新增一个学生
 3 new StudentSet().Add(new Student()
 4 {
 5     StuCode = "A0001",
 6     StuName = "张三",
 7     Sex = SexEnum.Male,
 8     Height = 1.73f,
 9     Birthday = new System.DateTime(1999, 12, 3)
10 });
11 
12 //批量新增,返回成功新增的条数
13 var successAddCount = new StudentSet().BatchAdd(new List<Student>()
14 {
15     new Student(){
16         StuCode = "A0002",
17         StuName = "李四"
18     },
19     new Student(){
20       StuCode = "A0003",
21       StuName = "王老五"
22     }
23 });
24 
25 //删除学生姓名为"李四"的学生,返回删除成功的条数
26 var successDeleteCout = new StudentSet().RemoveAll(p => p.StuName == "李四");
27 
28 //修改学号为"A0001"学生的身高为1.8,返回修改成功的条数
29 var successUpdateCount= new StudentSet().Update(p => new { Height = 1.8f }, p => p.StuCode == "A0001");
30 
31 //查询学号为"A0001"的第一条学生数据,如果没有返回默认值
32 var student = new StudentSet().Where(p => p.StuCode == "A0001").FirstOrDefault();
33 
34 //查询学号为"A0001"的学生查询的姓名
35 var studentName = new StudentSet().Where(p => p.StuCode == "A0001").Select(p=>p.StuName).FirstOrDefault();
View Code

4.事务方式(基本操作)

 1 //创建一个事务操作
 2 using (var exe = DataContextFactory.CreateExecutor())
 3 {
 4     //开启事务
 5     exe.BeginTransaction();
 6     try
 7     {
 8         //新增一个学生,第2个参数事务参数
 9         new StudentSet().Add(new Student()
10         {
11             StuCode = "A0001",
12             StuName = "张三",
13             Sex = SexEnum.Male,
14             Height = 1.73f,
15             Birthday = new System.DateTime(1999, 12, 3)
16         }, exe);
17 
18         //删除学生姓名为"李四1"的学生,返回删除成功的条数,第2个参数事务参数
19         var successDeleteCout_1 = new StudentSet().RemoveAll(p => p.StuName == "李四1", exe);
20         
21         //提交事务
22         exe.CommitTransaction();
23     }
24     catch
25     {
26         //异常时,事务回滚
27         exe.RollbackTransaction();
28     }
29 }
View Code

5.多表查询

 1 //查询学号为"A00001"的学生课程信息
 2 var stuCous = (from t in new StudentSet().AsQueryable()
 3                join t1 in new StudentCourseSet().AsQueryable() on t.StuCode equals t1.StuCode
 4                join t2 in new CourseSet().AsQueryable() on t1.CourseCode equals t2.CourseCode
 5                where t.StuCode == "A0001"
 6                select new
 7                {
 8                    t.StuCode,
 9                    t.StuName,
10                    t1.CourseCode,
11                    t2.CourseName
12                }).ToList();
View Code

 

未完待续...

posted on 2025-07-15 14:38  唐扬名  阅读(11)  评论(1)    收藏  举报

导航