首页 > 代码库 > Entity Framework自用的DataBase基类

Entity Framework自用的DataBase基类

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Model; 6 using System.Data.Objects.DataClasses; 7  8 namespace Work 9 {10     public class DataBase<T> where T : EntityObject11     {12         protected OfficeAnywhereEntities entities = new OfficeAnywhereEntities();13 14         /// <summary>15         /// 添加16         /// </summary>17         /// <param name="t"></param>18         /// <returns></returns>19         public bool Add(T t)20         {21             entities.AddObject(typeof(T).Name, t);22             return SaveChanges() > 0;23         }24 25         /// <summary>26         /// 执行27         /// </summary>28         /// <returns></returns>29         private int SaveChanges()30         {31             try32             {33                 return entities.SaveChanges();34             }35             catch (Exception e)36             {37                 throw e.InnerException;38             }39             40         }41 42         /// <summary>43         /// 寻找对象44         /// </summary>45         /// <param name="Guid"></param>46         /// <returns></returns>47         private T FindByGuid(string Guid)48         {49             T t = null;50             entities.CreateObjectSet<T>().ToList().ForEach(p => {51                 if (p.EntityKey.EntityKeyValues[0].Value.ToString() == Guid)52                     t = p;53             });54             return t;55         }56 57         /// <summary>58         /// 删除59         /// </summary>60         /// <param name="Guid"></param>61         /// <returns></returns>62         public bool DelByGuid(string Guid)63         {64             T t = FindByGuid(Guid);65             if(t!=null)66             entities.DeleteObject(t);67             return SaveChanges() > 0;68         }69 70         /// <summary>71         /// 更新72         /// </summary>73         /// <param name="Guid"></param>74         /// <param name="act"></param>75         /// <returns></returns>76         public bool UpdateByGuid(string Guid, Action<T> act)77         {78             T t = FindByGuid(Guid);79             act(t);80             return SaveChanges() > 0;81         }82 83         /// <summary>84         /// 列表85         /// </summary>86         /// <returns></returns>87         public List<T> GetList()88         {89             return entities.CreateObjectSet<T>().ToList();90         }91     }92 }

 

Entity Framework自用的DataBase基类