public class CURDEntity<T> where T : BaseEntity { private MyContext ctx; public CURDEntity(MyContext UserContext) { this.ctx = UserContext; } /// <summary> /// 获取所有数据 /// </summary> /// <returns></returns> public IQueryable<T> GetAll() { var allData= this.ctx.Set<T>().Where<T>(t => t.IsDeleted == false); return allData; //return ctx.<T> } public T GetTById(long Id) { var SingleData = this.ctx.Set<T>().Where(t => t.Id == Id).SingleOrDefault(); return SingleData; } public IQueryable<T> GetDataByPager(int PageIndex, int PageSize) { return this.ctx.Set<T>().OrderBy(t=>t.CreateDateTime).Skip(PageIndex).Take(PageSize); } public bool DeleteById(int Id) { try { GetTById(Id).IsDeleted = false; this.ctx.SaveChanges(); return true; } catch { return false; } }
public abstract class BaseEntity { public long Id { get; set; } public DateTime CreateDateTime { get; set; } public bool IsDeleted { get; set; } } public class Users:BaseEntity { public string Name { get; set; } public string LoginName { get; set; } public string LoginPassWord { get; set; } public string Email { get; set; } public int? Age { get; set; } public string Birthday { get; set; } public string Phone { get; set; } }