BaseDal类:

    public class BaseDal : IDisposable
{
#region 事件
internal Func<bool> DBCommitting;
internal Action<bool> DBCommitted; internal Func<object, bool> DBAdding;
internal Action<object, bool> DBAdded;
internal Func<object, bool> DBUpdating;
internal Action<object, bool> DBUpdated;
internal Func<object, bool> DBDeleting;
internal Action<object, bool> DBDeleted; internal Func<object, bool, bool> DBSaveOrUpdating;
internal Action<object, bool, bool> DBSaveOrUpdated;
internal Func<object, bool, bool> DBSaveOrUpdatingForList;
internal Action<object, bool, bool> DBSaveOrUpdatedForList;
#endregion private DBEntities db = new DBEntities();//实体模型名 private bool isTransaction = false; public bool IsBeginTranscation { get { return isTransaction; } } public IQueryable<T> GetEntity<T>() where T : class
{
return db.Set<T>().AsNoTracking().AsQueryable<T>();
} /// <summary>
/// 添加
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="lst"></param>
/// <returns></returns>
public bool Add<T>(List<T> lst, bool isCommit = true) where T : class
{
foreach (var item in lst)
{
db.Entry<T>(item).State = System.Data.EntityState.Added;
} if (isCommit && !isTransaction)
{
bool isContinue = true;
if (DBAdding != null) isContinue = DBAdding(lst);
if (isContinue)
{
var ret = db.SaveChanges() > ;
if (DBAdded != null) DBAdded(lst, ret); return ret;
}
else
return false;
}
else
return false; } /// <summary>
/// 根据条件删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public bool DeleteByCondition<T>(Expression<Func<T, bool>> predicate, bool isCommit = true) where T : class
{
db.Set<T>().Where<T>(predicate).ToList<T>().ForEach(d =>
{
db.Entry<T>(d).State = System.Data.EntityState.Deleted;
});
if (isCommit && !isTransaction)
{
bool isContinue = true;
if (DBDeleting != null) isContinue = DBDeleting(predicate);
if (isContinue)
{
var ret = db.SaveChanges() > ;
if (DBDeleted != null) DBDeleted(predicate, ret);
return ret;
}
else
return false;
}
else
return false;
} public bool UpdateByCondition<T>(Action<T> updateExpression, Expression<Func<T, bool>> predicate, bool isCommit = true) where T : class
{
var lstDB = db.Set<T>().Where<T>(predicate).ToList<T>();
var lstEF = db.Set<T>().Local.AsQueryable().Where(predicate).ToList<T>();
lstDB.AddRange(lstEF);
lstDB.ForEach(item =>
{
updateExpression(item);
if (db.Entry<T>(item).State != EntityState.Deleted && db.Entry<T>(item).State != EntityState.Added)
db.Entry<T>(item).State = EntityState.Modified;
}); if (isCommit && !isTransaction)
{
bool isContinue = true;
if (DBUpdating != null) isContinue = DBUpdating(predicate);
if (isContinue)
{
var ret = db.SaveChanges() > ;
if (DBUpdated != null) DBUpdated(predicate, ret); return ret;
}
else
return false;
}
else
{
return false;
}
} public bool SaveOrUpdate<T>(T entity, bool isAdd = false, bool isCommit = true) where T : class
{
if (isAdd)
db.Set<T>().Add(entity);
else
db.Entry(entity).State = System.Data.EntityState.Modified; if (isCommit && !isTransaction)
{
bool isContinue = true;
if (DBSaveOrUpdating != null) isContinue = DBSaveOrUpdating(entity, isAdd);
if (isContinue)
{
var ret = db.SaveChanges() > ;
if (DBSaveOrUpdated != null) DBSaveOrUpdated(entity, isAdd, ret);
return ret;
}
else
return false;
}
else
return false; } public bool SaveOrUpdateForList<T>(List<T> entities, bool isAdd = false, bool isCommit = true) where T : class
{
foreach (T entity in entities)
{
if (isAdd)
db.Set<T>().Add(entity);
else
db.Entry(entity).State = System.Data.EntityState.Modified;
}
if (isCommit && !isTransaction)
{
bool isContinue = true;
if (DBSaveOrUpdatingForList != null) isContinue = DBSaveOrUpdatingForList(entities, isAdd);
if (isContinue)
{
var ret = db.SaveChanges() > ;
if (DBSaveOrUpdatedForList != null) DBSaveOrUpdatedForList(entities, isAdd, ret); return ret;
}
else
return false;
}
else
return false;
} public int ExecuteSqlCommand(string sql, bool isCommit = true)
{
db.Database.ExecuteSqlCommand(sql);
if (isCommit && !isTransaction)
return db.SaveChanges();
else
return ;
} public int ExecuteSqlCommand(string sql, bool isCommit, params object[] parameters)
{
db.Database.ExecuteSqlCommand(sql, parameters);
if (isCommit && !isTransaction)
return db.SaveChanges();
return ;
} /// <summary>
/// 执行存储过程 无超时
/// </summary>
/// <param name="ProcName"></param>
/// <param name="parList"></param>
public void ExecPro(string ProcName, List<SqlParameter> parList)
{
DbConnection conn = db.Database.Connection;
if (conn.State == ConnectionState.Closed)
conn.Open();
DbCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = ProcName;
cmd.CommandTimeout = ;
foreach (SqlParameter par in parList)
{
cmd.Parameters.Add(par);
} cmd.ExecuteNonQuery();
} public void BeginTranscation()
{
isTransaction = true;
} public bool Commit()
{
if (isTransaction)
{
bool isContinue = true;
isTransaction = false;
if (DBCommitting != null) isContinue = DBCommitting();
if (isContinue)
{
var ret = db.SaveChanges() > ;
if (DBCommitted != null) DBCommitted(ret);
return ret;
}
else
return false;
}
else
return false;
} /// <summary>
/// 执行SQL查询语句
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public List<T> FindEntityListBySql<T>(string sql)
{
return db.Database.SqlQuery<T>(sql).ToList<T>();
} public void Close()
{
if (db != null)
{
db.Dispose();
db = null;
}
} public void Dispose()
{
if (db != null)
{
db.Dispose();
db = null;
}
}
}

DBEvent类

    public class DBEvent<T> where T : class
{
protected BaseDal db; public DBEvent(BaseDal db)
{
this.db = db;
db.DBUpdating = Updating;
db.DBUpdated = Updated;
db.DBAdding = Adding;
db.DBAdded = Added;
db.DBDeleting = Deleting;
db.DBDeleted = Deleted;
db.DBSaveOrUpdating = SaveOrUpdating;
db.DBSaveOrUpdated = SaveOrUpdated;
db.DBSaveOrUpdatingForList = SaveOrUpdatingForList;
db.DBSaveOrUpdatedForList = SaveOrUpdatedForList;
db.DBCommitting = Commintting;
db.DBCommitted = Committed;
} public delegate bool BeforeUpdateEvent(Expression<Func<T, bool>> predicate); public delegate void AfterUpdateEvent(Expression<Func<T, bool>> predicate, bool isSuccess); public delegate bool BeforeAddEvent(List<T> lstEntity); public delegate void AfterAddEvent(List<T> lstEntity, bool isSuccess); public delegate bool BeforeDeleteEvent(Expression<Func<T, bool>> predicate); public delegate void AfterDeleteEvent(Expression<Func<T, bool>> predicate, bool isSuccess); public delegate bool BeforeSaveOrUpdateEvent(T entity, bool isAdd); public delegate void AfterSaveOrUpdateEvent(T entity, bool isAdd, bool isSuccess); public delegate bool BeforeSaveOrUpdatingForListEvent(List<T> lstEntity, bool isAdd); public delegate bool AfterSaveOrUpdatingForListEvent(List<T> lstEntity, bool isAdd, bool isSuccess); public delegate bool BeforeCommitEvent(); public delegate void AfterCommitEvent(bool isSuccess); public BeforeUpdateEvent BeforeUpdate { set; get; } public AfterUpdateEvent AfterUpdate { set; get; } public BeforeAddEvent BeforeAdd { get; set; } public AfterAddEvent AfterAdd { get; set; } public BeforeDeleteEvent BeforeDelete { get; set; } public AfterDeleteEvent AfterDelete { get; set; } public BeforeSaveOrUpdateEvent BeforeSaveOrUpdate { get; set; } public AfterSaveOrUpdateEvent AfterSaveOrUpdate { get; set; } public BeforeSaveOrUpdatingForListEvent BeforeSaveOrUpdatingForList { get; set; } public AfterSaveOrUpdatingForListEvent AfterSaveOrUpdatingForList { get; set; } public BeforeCommitEvent BeforeCommit { get; set; } public AfterCommitEvent AfterCommit { get; set; } private bool Updating(object arg)
{
Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>;
if (BeforeUpdate != null)
return BeforeUpdate(predicate);
else
return true;
} private void Updated(object arg, bool isSuccess)
{
Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>;
if (AfterUpdate != null)
AfterUpdate(predicate, isSuccess);
} private bool Adding(object arg)
{
List<T> lst = arg as List<T>;
if (BeforeAdd != null)
return BeforeAdd(lst);
else
return true;
} private void Added(object arg, bool isSuccess)
{
List<T> lst = arg as List<T>;
if (AfterAdd != null)
AfterAdd(lst, isSuccess);
} private bool Deleting(object arg)
{
Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>;
if (BeforeDelete != null)
return BeforeDelete(predicate);
else
return true;
} private void Deleted(object arg, bool isSuccess)
{
Expression<Func<T, bool>> predicate = arg as Expression<Func<T, bool>>;
if (AfterDelete != null)
AfterDelete(predicate, isSuccess);
} private bool SaveOrUpdating(object arg, bool isAdd)
{
var entity = arg as T;
if (BeforeSaveOrUpdate != null)
return BeforeSaveOrUpdate(entity, isAdd);
else
return true;
} private void SaveOrUpdated(object arg, bool isAdd, bool isSuccess)
{
var entity = arg as T;
if (AfterSaveOrUpdate != null)
AfterSaveOrUpdate(entity, isAdd, isSuccess);
} private bool SaveOrUpdatingForList(object arg, bool isAdd)
{
List<T> lstEntity = arg as List<T>;
if (BeforeSaveOrUpdatingForList != null)
return BeforeSaveOrUpdatingForList(lstEntity, isAdd);
else
return false;
} private void SaveOrUpdatedForList(object arg, bool isAdd, bool isSuccess)
{
List<T> lstEntity = arg as List<T>;
if (AfterSaveOrUpdatingForList != null)
AfterSaveOrUpdatingForList(lstEntity, isAdd, isSuccess);
} private bool Commintting()
{
if (BeforeCommit != null)
return BeforeCommit();
else
return false;
} private void Committed(bool isSuccess)
{
if (AfterCommit != null)
AfterCommit(isSuccess);
} }

ViewEntityDependent类

 public static class ViewEntityDependent
{
public static void RegisterEntityDependent()
{
JobView();
} private static void JobView()
{
List<string> jobRecord = new List<string>()
{
"JobResumeAutonomy","JobResumeAttention","JobResumeCompanyAuto","JobResumeCompanyPut"
};
CacheDependentEntitySet.RegisterDependentEntity("JobRecord", jobRecord);
} }

让EF支持sql语句的更多相关文章

  1. MVC EF 执行SQL语句(转载)

    MVC EF 执行SQL语句 最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 闲着没事,看了一篇关于LI ...

  2. easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)

    easyui datagrid 禁止选中行   没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...

  3. MVC EF 执行SQL语句

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 闲着没事,看了一篇关于LINQ和SQL对比的文章,网友们 ...

  4. 如何用ORM支持SQL语句的CASE WHEN?

    OQL如何支持CASE WHEN? 今天,一个朋友问我,OQL可否支持CASE WHEN语句?他给的示例SQL如下: then '启用' else '停用' from tb_User OQL是SOD框 ...

  5. ASP.NET MVC5+EF6+EasyUI 后台管理系统(89)-EF执行SQL语句与存储过程

    这一节,我们来看看EF如何执行SQL语句与读取存储过程的数据,可能有一部分人,还不知道EF如何执行存储过程与原生SQL语句! 我们什么时候要直接使用原生的SQL语句? 返回值过于复杂 过于复杂的联合查 ...

  6. 【Swift 4.0】扩展 WCDB 支持 SQL 语句

    前言 入坑 wcdb 有两个月了,整体来说还是很不错的,具体优点可以参考文档说明,由于官方明确说明不支持 SQL 只好自己写一个扩展支持一下了

  7. EF执行SQL语句

    使用上下文中的Database.SqlQuery<对应的表名>(sql语句) var data = dbcenter.Database.SqlQuery<CcBusiFormview ...

  8. EF使用sql语句

    https://www.cnblogs.com/chenwolong/p/SqlQuery.html https://blog.csdn.net/zdhlwt2008/article/details/ ...

  9. ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪

    ASP.NET MVC深入浅出(被替换)   一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...

随机推荐

  1. EventLog监控

    http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/logs/eventlogs/ http://etuto ...

  2. Webbrowser中模拟连接点击(非鼠标模拟)

    Delphi uses mshtml, ActiveX; //初始加载网易主页 procedure TForm1.FormCreate(Sender: TObject); begin Webbrows ...

  3. ubuntu 设置网卡为混杂模式 以及网络配置命令

    1. ifconfig eth0 promisc 设置eth0为混杂模式. ifconfig eth0 -promisc 取消它的混杂模式 botnet@botnet-virtual-machine: ...

  4. 使用CocoaPods找不到头文件解决方法

    Project->Info->Configurations

  5. strcpy()、memcpy()、memmove()、memset()的内部实现

    一直想知道 strcpy().memcpy().memmove().memset()的内部实现 strcpy(), 字符串拷贝. char *strcpy(char *strDest, const c ...

  6. OAuth2 for asp.net web api

    在上篇文章中我研究了OpenId及DotNetOpenAuth的相关应用,这一篇继续研究OAuth2. https://github.com/DotNetOpenAuth http://www.cnb ...

  7. 对浏览器css兼容性的学习理解及问题解决汇总

    一.从浏览器内核的角度 来看,浏览器兼容性问题可分为以下三类: 1. 渲染相关:和样式相关的问题,即体现在布局效果上的问题. 2. 脚本相关:和脚本相关的问题,包括JavaScript和DOM.BOM ...

  8. Programming C#.Classes and Objects.只读字段

    只读字段 当字段声明中含有 readonly 修饰符时,该声明所引入的字段为只读字段.给只读字段的直接赋值只能作为声明的组成部分出现,或在同一类中的实例构造函数或静态构造函数中出现.(在这些上下文中, ...

  9. Linux网络管理——网关作用

    1. 网络基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...

  10. 练习-checkbox 全选 ,反选, 单选,以及取值

    1.方法1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w ...