Rainr.EFClient – EntityFramework封装简洁操作类库

Rainr.EFClient

自己闲暇时写的一个基于EF操作数据库的类库 。感觉用起来还不错。分享给大家。有兴趣的可以试试 呵呵:)

功能:

  更加简洁方便的用实体与数据库的交互。

环境:

  EntityFramework 5.0 [.Net Framework 4.0]

注意:

  一个DbAccessor对象只能被一个线程操作。

演示:

1 using System;
2 using System.Linq;
3 using System.Data.Entity;
4 using System.Collections.Generic;
5 using Rainr.EFClient;
6
7 namespace ConsoleApplication
8 {
9 /*
10 实体类
11 */
12 public class TestUser
13 {
14 public int Id { get; set; }
15 public string Name { get; set; }
16 public int Age { get; set; }
17 }
18
19
20 /*
21 数据上下文
22 */
23 public class TestContext : DbContext
24 {
25 public TestContext() : base(TextConnection) { }
26
27 public DbSet<TestUser> TestUser { get; set; } //确保名称和类别一致,如果不一致无法更新非代理对象
28 }
29
30
31 /*
32 使用演示
33 */
34 class Program
35 {
36 static void Main(string[] args)
37 {
38 //访问者
39 DbAccessor<TestContext> accessor = new DbAccessor<TestContext>(); // == accessor = DbAccessor.NewAccessor<TestContext>();
40
41 //—–查询——//
42 TestUser selectUser = null;
43 List<TestUser> selectUsers = null;
44 //根据主键值查询单个对象
45 selectUser = accessor.SelectByKey<TestUser>(1);
46 //根据参数查询单个对象
47 selectUser = accessor.Select<TestUser>(u => u.Id == 1);
48 //查询多个对象
49 selectUsers = accessor.Selects<TestUser>().ToList();
50 //根据参数查询多个对象
51 selectUsers = accessor.Selects<TestUser>(u => u.Id > 0 && u.Name == testName).ToList();
52 //无跟踪,排序,分页,查询多个对象
53 int rowTotal = 0;
54 selectUsers = accessor.Selects<TestUser>(u => u.Id > 0).OrderBy(Id).Pagination(1, 20, out rowTotal).AsNoTracking().ToList();
55
56 //—–插入——//
57 TestUser insertUser = new TestUser() { Name = insert.. };
58 //插入对象
59 accessor.Insert<TestUser>(insertUser);
60
61 //—–更新——//
62 TestUser updateProxyUser = accessor.SelectByKey<TestUser>(1);
63 TestUser updateUser = new TestUser() { Id = 1, Name = uuuuu };
64 //更新代理对象
65 accessor.Update<TestUser>(updateProxyUser);
66 //更新非代理对象
67 accessor.Update<TestUser>(updateUser);
68 //更新对象某些属性
69 accessor.UpdateProperty<TestUser>(updateUser, Name, Age);//..可指定多个
70 //更新对象属性排除某些属性
71 accessor.UpdatePropertyExcluded<TestUser>(updateUser, Age);//..可指定多个
72
73 //—–删除——//
74 TestUser deleteProxyUser = accessor.SelectByKey<TestUser>(1);
75 TestUser deleteUser = new TestUser() { Id = 1, Name = dddd };
76 //根据主键删除对象
77 accessor.DeleteByKey<TestUser>(1);
78 //删除代理对象
79 accessor.Delete<TestUser>(deleteProxyUser);
80 //删除非代理对象
81 accessor.Delete<TestUser>(deleteUser);
82
83 //—–直接Linq 操作——//
84 var linqUsers = (from users in accessor.Context.TestUser select users);
85
86 //—–事务——//
87 //开始事务
88 accessor.BeginTransaction();
89 //中间多个 操作。。。。
90 //提交事务
91 accessor.SubmitTransaction();
92
93 //—–分布式事务:有待测试——//
94 DbAccessor<TestContext> accessone = new DbAccessor<TestContext>();
95 DbAccessor<OtherContext> accesstwo = new DbAccessor<OtherContext>();
96 DbAccessorTransaction dba = new DbAccessorTransaction();
97 dba.EnlistAccessor<DbAccessor<TestContext>>(accessone);//加入访问第一个数据库的访问器
98 dba.EnlistAccessor<DbAccessor<OtherContext>>(accesstwo);//加入访问别的数据库的访问器
99 dba.SubmitTransaction();//提交分布式事务
100
101 }
102 }
103 }

 

下载:

Rainr.EFClient.Demo.rar[百度盘]  

 

By Rainr(2012-12-31) 

本文链接



You must enable javascript to see captcha here!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress

无觅相关文章插件,快速提升流量