using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.Entity;using System.Data.Entity.Core.Objects;using System.Data.Entity.Infrastructure;using System.Data.Entity.Migrations;using System.Data.Entity.Validation;using System.Linq;using System.Linq.Expressions;using System.Reflection;using System.Runtime.InteropServices;using System.Text;using EntityFramework.Extensions;using System.Threading.Tasks;//using log4net;//using log4net.Core;namespace _6._0Test{ ////// 上下文静态实体类 /// public static class DbContentEntity { ////// 静态初始化 /// static DbContentEntity() { if (string.IsNullOrEmpty(ConnectionString)) return; if (_entities == null) { _entities = new TestEntities(ConnectionString); _entities.Configuration.ValidateOnSaveEnabled = false; } if (_entities.Database.Connection.State == ConnectionState.Closed && _entities.Database.Connection.State != ConnectionState.Connecting) { _entities.Database.Connection.Open(); } } ////// 静态连接字符串 /// public static string ConnectionString { get { return ConfigurationManager.ConnectionStrings["TestEntities"].ConnectionString; } } ////// 静态实体 /// private static TestEntities _entities; ////// 对外实体 /// public static TestEntities Entities { get { if (string.IsNullOrEmpty(ConnectionString)) return null; return _entities ?? (_entities = new TestEntities(ConnectionString)); } } } ////// 上下文类 /// ///public abstract class ModelContext where T : class { //日志,可自行拓展 //public static ILog Log = LogManager.GetLogger(typeof(T).Name); /// /// 返回上下文实体 /// protected TestEntities Entities { get { return DbContentEntity.Entities; } } private DbSet_model; protected DbSet Model { get { return _model ?? (_model = Entities.Set ()); } } #region =====查询用方法===== /// /// 根据条件查询实体 /// /// 条件 ///实体 public virtual T Get(Expression> @where) { return Model.FirstOrDefault(where); } /// /// 根据条件查询实体(异步) /// /// 条件 ///实体 public async virtual TaskGetAsync(Expression > @where) { return await Model.FirstOrDefaultAsync(where); } /// /// 根据条件查询实体集合(需自行tolist,自行异步) /// /// 查询条件 ///public virtual IQueryable GetList(Expression > @where) { return Model.AsNoTracking().Where(where); } /// /// 根据条件查询实体集合(需自行tolist,自行异步) /// /// 查询条件 ///public async virtual Task > GetListAsync(Expression > @where) { return await Task.Run(()=>Model.AsNoTracking().Where(where)); } /// /// 根据传进来的实体类型查询该实体的集合 /// ///实体类型 /// 查询条件 ///public virtual IQueryable GetList (Expression > @where) where TM : class { var model = Entities.Set (); return model.AsNoTracking().Where(where); } /// /// 根据传进来的实体类型查询该实体(异步) /// ///实体类型 /// 查询条件 ///public async virtual Task GetAsync (Expression > @where) where TM : class { var model = Entities.Set (); return await model.FirstOrDefaultAsync(where); } /// /// 根据条件查询实体数量 /// ///实体类型 /// 查询条件 ///public virtual int Cout (Expression > @where) where TM : class { try { var model = Entities.Set (); return model.AsNoTracking().Count(where); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception ex) { //Log.Error(ex + ex.Message); return -1; } } /// /// 根据条件查询实体数量(异步) /// ///实体类型 /// 查询条件 ///public async virtual Task CoutAsync (Expression > @where) where TM : class { try { var model = Entities.Set (); return await model.AsNoTracking().CountAsync(where); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception ex) { //Log.Error(ex + ex.Message); return -1; } } /// /// 根据条件查询实体数量 /// /// ///public async virtual Task Count(Expression > @where) { try { var model = Entities.Set (); return await model.AsNoTracking().CountAsync(where); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception ex) { //Log.Error(ex + ex.Message); return -1; } }#endregion #region ====添加用方法==== /// /// 添加实体 /// /// ///public virtual bool Add(T m) { try { Model.Add(m); Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 添加实体(异步) /// /// ///public async virtual Task AddAsync(T m) { try { Model.Add(m); await Entities.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 根据类型添加实体 /// ////// /// public virtual bool Add (TM tm) where TM : class { try { var model = Entities.Set (); model.Add(tm); Entities.SaveChanges(); } catch (DbEntityValidationException dbex) { //Log.Error(dbex + dbex.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 批量添加实体 /// /// ///public virtual bool AddRange(IEnumerable m) { try { Model.AddRange(m); Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 批量添加实体(异步) /// /// ///public async virtual Task AddRangeAsync(IEnumerable m) { try { Model.AddRange(m); await Entities.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } #endregion #region ====修改用方法==== /// /// 修改实体 /// /// ///public virtual bool Update(T t) { try { Model.Attach(t); Entities.Entry(t).State = EntityState.Modified; Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 修改实体(异步) /// /// ///public async virtual Task UpdateAsync(T t) { try { Model.Attach(t); Entities.Entry(t).State = EntityState.Modified; await Entities.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 根据类型修改实体 /// ////// /// public virtual bool Update (TM tm) where TM : class { try { var model = Entities.Set (); model.Attach(tm); Entities.Entry(tm).State = EntityState.Modified; Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 批量更新数据 /// /// 更新数据的条件 如:u => u.FirstName == "firstname" /// 更新的值 如:u=>new User{FirstName = "newfirstname"} ///返回影响的条数 public virtual int Update(Expression> @where, Expression > ex) { try { return Model.Where(where).Update(ex); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception e) { // Log.Error(e + e.Message); return -1; } } /// /// 批量更新数据(异步) /// /// 更新数据的条件 如:u => u.FirstName == "firstname" /// 更新的值 如:u=>new User{FirstName = "newfirstname"} ///返回影响的条数 public async virtual Task UpdateAsync(Expression> @where, Expression > ex) { try { return await Model.Where(where).UpdateAsync(ex); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception e) { // Log.Error(e + e.Message); return -1; } } /// /// 添加或者修改实体(无则添加,有则修改) /// /// ///public virtual bool AddOrUpdate(T[] m) { try { Model.AddOrUpdate(m); Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { // Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 添加或者修改实体(无则添加,有则修改,异步) /// /// ///public async virtual Task AddOrUpdateAsync(T[] m) { try { Model.AddOrUpdate(m); await Entities.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { // Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } #endregion #region ===事务==== /// /// 运行基本事务,返回bool值 /// /// ///protected bool RunTransaction(Action > model) { using (var transaction = Entities.Database.BeginTransaction()) { try { model.Invoke(Model); Entities.SaveChanges(); transaction.Commit(); return true; } catch (DbEntityValidationException dbEx) { //Log.Error("EXEC RunTransaction Error:" + dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error("EXEC RunTransaction Error:" + ex + ex.Message); transaction.Rollback(); return false; } } } /// /// 运行基本事务,返回bool值(异步) /// /// ///protected async Task RunTransactionAsync(Action > model) { using (var transaction = Entities.Database.BeginTransaction()) { try { model.Invoke(Model); await Entities.SaveChangesAsync(); transaction.Commit(); return true; } catch (DbEntityValidationException dbEx) { //Log.Error("EXEC RunTransaction Error:" + dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error("EXEC RunTransaction Error:" + ex + ex.Message); transaction.Rollback(); return false; } } } #endregion #region======删除用方法==== /// /// 删除实体 /// /// ///public virtual bool Remove(T t) { try { Model.Remove(t); Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 删除实体(异步) /// /// ///public async virtual Task RemoveAsync(T t) { try { Model.Remove(t); await Entities.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// /// 根据类型删除实体 /// ////// /// public virtual bool Remove (TM m) where TM : class { try { var model = Entities.Set (); model.Remove(m); Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return false; } catch (Exception ex) { //Log.Error(ex + ex.Message); return false; } return true; } /// ///批量删除实体 /// /// ///public virtual int RemoveRange(IEnumerable m) { try { Model.RemoveRange(m); return Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception ex) { //Log.Error(ex + ex.Message); return -1; } } /// ///批量删除实体(异步) /// /// ///public async virtual Task RemoveRangeAsync(IEnumerable m) { try { Model.RemoveRange(m); return await Entities.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception ex) { //Log.Error(ex + ex.Message); return -1; } } /// /// 根据类型批量删除实体 /// ////// /// public virtual int RemoveRange (IEnumerable tmMs) where TM : class { try { var model = Entities.Set (); model.RemoveRange(tmMs); return Entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { //Log.Error(dbEx + dbEx.Message); return -1; } catch (Exception ex) { //Log.Error(ex + ex.Message); return -1; } } #endregion }}
..求大神指点一下..哪里有问题..我在改改..