EF通用CRED
public class Operation:IDisposable { private EFContext ef { get { return new EFContext(); } } /// <summary> /// 添加一个实体对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public void Add<T>(T t) where T:class { ef.Set<T>().Add(t); ef.SaveChanges(); } /// <summary> /// 释放掉EFContext /// </summary> public void Dispose() { if (ef != null) { ef.Dispose(); } } /// <summary> /// 获取实体对象的条数 /// </summary> /// <typeparam name="T">实体对象类</typeparam> /// <param name="t">对象实例</param> /// <returns></returns> public int Count<T>(T t) where T : class { return ef.Set<T>().Count<T>(); } /// <summary> /// 获取实体对象的所有集合 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <returns></returns> public IQueryable<T> GetList<T>() where T : class { return ef.Set<T>().AsQueryable<T>(); } /// <summary> /// 获取实体对象的所有集合 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="start">开始</param> /// <param name="count">行数size</param> /// <returns></returns> public IQueryable<T> GetLsit<T>(int start, int count)where T:class { return ef.Set<T>().Skip(start).Take(count); } /// <summary> /// 根据条件返回一个唯一的实体对象 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="p">表达式</param> /// <returns></returns> public T Get<T>(System.Linq.Expressions.Expression<Func<T, bool>> p) where T : class { return ef.Set<T>().SingleOrDefault<T>(p); } /// <summary> /// 删除指定的实体对象 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="t">删除的实例对象</param> /// <returns>返回这个实例对象</returns> public T Delete<T>(T t) where T : class { ef.Set<T>().Remove(t); ef.SaveChanges(); return t; } /// <summary> /// 更新一个实体对象 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="t">更新的实例对象</param> /// <returns>返回这个实例对象</returns> public T UpDate<T>(T t) where T : class { ef.Entry<T>(t).State = System.Data.Entity.EntityState.Detached; ef.SaveChanges(); return t; } }