在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、EF(Entity Framework) //创建EF上下文 Model.ModelContainer db = new Model.ModelContainer(); //下面有线程内实例唯一的例子 UserInfo info = new UserInfo(); info.Name = "张三"; db.UserInfo.Add(info); //添加数据 db.SaveChanges(); //最后必须SaveChanges()才能保存,把状态的实体映射回数据库。 //info.Id = 1; //修改或删除操作,必须要有主键。 //db.Entry<T>(entity).State = EntityState.Added; //添加 //db.Entry<T>(entity).State = EntityState.Modified; //修改 //db.Entry<T>(entity).State = EntityState.Deleted; //删除
public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>().AddObject(entity); //EF5.0的写法 db.Entry<T>(entity).State = EntityState.Added; //下面的写法统一 db.SaveChanges(); return entity; } public bool UpdateEntity(T entity) { //EF4.0的写法 //db.CreateObjectSet<T>().Addach(entity); //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); //EF5.0的写法 db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Modified; return db.SaveChanges() > 0; } public bool DeleteEntity(T entity) { //EF4.0的写法 //db.CreateObjectSet<T>().Addach(entity); //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted); //EF5.0的写法 db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Deleted; return db.SaveChanges() > 0; } public IQueryable<T> LoadEntities(Func<T, bool> whereLambda) { //EF4.0的写法 //return db.CreateObjectSet<T>().Where<T>(whereLambda).AsQueryable(); //EF5.0的写法 return db.Set<T>().Where<T>(whereLambda).AsQueryable(); } 执行SQL语句 //EF4.0的写法 public int ExcuteSql(string sql, params ObjectParameter[] parameters) { return DbContext.ExecuteFunction(sql, parameters); } //EF5.0的写法 public int ExecuteSql(string sql, params System.Data.SqlClient.SqlParameter[] pars) { return DbContext.Database.ExecuteSqlCommand(sql, pars); }
public class BaseRepository<T> where T:class,new () { private DbContext DbContext { get { return DbContextFactory.CreateDbContext(); //创建唯一实例。 } } //简单查询 public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda) { return DbContext.Set<T>().Where(whereLambda).AsQueryable(); } //分页查询 *** public IQueryable<T> LoadPageEntities<S>(int pageSize, int pageIndex, out int totalCount, Expression<Func<T, bool>> whereLambda, bool isAsc, Expression<Func<T, S>> orderBy) { IQueryable<T> result = DbContext.Set<T>().Where(whereLambda).AsQueryable(); totalCount = result.Count(); //返回总记录条数 if (isAsc) { result = result.OrderBy(orderBy) .Skip(pageSize * (pageIndex - 1)) .Take(pageSize) .AsQueryable(); } else { result = result.OrderByDescending(orderBy) .Skip(pageSize * (pageIndex - 1)) .Take(pageSize) .AsQueryable(); } return result; } public T Add(T entity) { DbContext.Set<T>().Add(entity); return entity; } public bool Detele(T entity) { DbContext.Entry(entity).State = EntityState.Deleted; return true; } public bool Update(T entity) { DbContext.Entry(entity).State = EntityState.Modified; return true; } }
/// <summary> /// 保证EF的上下文。线程内实例唯一,一次请求一个实例、 /// </summary> public static class DbContextFactory { public static DbContext CreateDbContext() { //线程内实例唯一 DbContext db = (DbContext)HttpContext.Current.Items["DbContext"]; if (db == null) { db = new Model.ModelContainer(); HttpContext.Current.Items.Add("DbContext", db); } return db; } }
|
请发表评论