1 public class CustomDataContext<TEntity> : System.Data.Linq.DataContext
2 where TEntity : class,new()
3 {
4 public CustomDataContext() :
5 base(ConfigurationManager.AppSettings["TestDbConnectionString"])
6 {
7 }
8
9 #region 创建一个新对象
10 public bool Create(TEntity entity)
11 {
12 EntityList.InsertOnSubmit(entity);
13 SubmitChanges();
14 return true;
15 }
16
17 public bool Create(IEnumerable<TEntity> entities)
18 {
19 EntityList.InsertAllOnSubmit(entities);
20 SubmitChanges();
21 return true;
22 }
23 #endregion
24
25 #region 删除一个对象
26 public bool Delete(TEntity entity)
27 {
28 EntityList.DeleteOnSubmit(entity);
29 SubmitChanges();
30 return true;
31 }
32
33 public bool Delete(IEnumerable<TEntity> entities)
34 {
35 EntityList.DeleteAllOnSubmit(entities);
36 SubmitChanges();
37 return true;
38 }
39 #endregion
40
41 #region 修改对象
42 public bool Modify(TEntity entity)
43 {
44 EntityList.Attach(entity, true);
45 SubmitChanges();
46 return true;
47 }
48
49 public bool Modify(IEnumerable<TEntity> entities)
50 {
51 EntityList.AttachAll(entities, true);
52 SubmitChanges();
53 return true;
54 }
55 #endregion
56
57 #region 查询对象
58 public TEntity GetEntity(Expression<Func<TEntity, bool>> predicate)
59 {
60 return EntityList.FirstOrDefault(predicate);
61 }
62 public IList<TEntity> GetList()
63 {
64 return EntityList.ToList();
65 }
66 public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> predicate)
67 {
68 return EntityList.Where(predicate);
69 }
70 #endregion
71
72 public System.Data.Linq.Table<TEntity> EntityList
73 {
74 get
75 {
76 return this.GetTable<TEntity>();
77 }
78 }
79 }