csharp EF增删改查封装
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp EF增删改查封装相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 公共EF增删改查
/// </summary>
public class BaseDal<T> where T : class, new()
{
public static WldxCmsEntities DbContext
{
get
{
return DbContextHelper.GetCurrentDbContext();
}
}
#region 添加
/// <summary>
/// 添加
/// </summary>
/// <typeparam name="T">要添加的对象类型</typeparam>
/// <param name="entity">要添加的对象</param>
/// <returns>是否成功</returns>
public static bool Insert(T entity)
{
DbContext.Set<T>().Add(entity);
return DbContext.SaveChanges() > 0;
}
/// <summary>
/// 添加
/// </summary>
/// <typeparam name="T">要添加的对象类型</typeparam>
/// <param name="entitys">要添加的对象集合</param>
/// <returns>返回添加成功数</returns>
public static int Insert(List<T> entitys)
{
foreach (var item in entitys)
{
DbContext.Set<T>().Add(item);
}
return DbContext.SaveChanges();
}
#endregion
#region 删除
/// <summary>
/// 删除
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="whereFunc">表达式树,用于筛选数据的条件</param>
/// <returns></returns>
public static int Delete(Expression<Func<T, bool>> whereFunc)
{
var list = DbContext.Set<T>().Where(whereFunc);
DbContext.Set<T>().RemoveRange(list);
return DbContext.SaveChanges();
}
#endregion
#region 查询
/// <summary>
/// 查找全部表数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public static IQueryable<T> Select()
{
return DbContext.Set<T>().AsQueryable();
}
/// <summary>
/// 查找数据
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="predicate">表达式树,用于筛选数据的条件</param>
/// <returns></returns>
public static IQueryable<T> Select(Expression<Func<T, bool>> predicate)
{
return DbContext.Set<T>().Where(predicate).AsQueryable();
}
/// <summary>
/// 返回对应表中的第一个元素;如果未找到这样的元素,则返回默认值(可为null的对象返回null,不可为null的对象返回默认初始值)。
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <returns></returns>
public static T FirstOrDefault()
{
return DbContext.Set<T>().FirstOrDefault();
}
/// <summary>
/// 返回对应表中满足条件的第一个元素;如果未找到这样的元素,则返回默认值(可为null的对象返回null,不可为null的对象返回默认初始值)。
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="predicate">表达式树,用于筛选数据的条件</param>
/// <returns></returns>
public static T FirstOrDefault(Expression<Func<T, bool>> predicate)
{
return DbContext.Set<T>().Where(predicate).FirstOrDefault();
}
#endregion
#region 修改
/// <summary>
/// 修改,可批量修改
/// </summary>
/// <typeparam name="T">模型</typeparam>
/// <param name="entity">存储要修改的值的对象</param>
/// <param name="whereFunc">使用表达式查找出需要修改的对象</param>
/// <param name="propertyNames">需要修改的对象的属性</param>
/// <returns></returns>
public static int Update(T entity, Expression<Func<T, bool>> whereFunc, params string[] propertyNames)
{
if (propertyNames == null || propertyNames.Length == 0) return 0;
var list = Select(whereFunc);
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase;
Type type = typeof(T);
foreach (var source in list)
{
DbContext.Entry(source).State = EntityState.Unchanged;
foreach (var name in propertyNames)
{
PropertyInfo info = type.GetProperty(name, bindingFlags);
if (info != null)
{
info.SetValue(source, info.GetValue(entity));
DbContext.Entry(source).Property(name).IsModified = true;
}
}
}
DbContext.Configuration.ValidateOnSaveEnabled = false;
return DbContext.SaveChanges();
}
/// <summary>
/// 修改,可批量修改
/// </summary>
/// <typeparam name="T">模型</typeparam>
/// <param name="whereFunc">根据委托查找需要修改的对象</param>
/// <param name="dic">要修改的属性和值得键值对集合</param>
/// <returns></returns>
public static int Update(Expression<Func<T, bool>> whereFunc, Dictionary<string, object> dic)
{
if (dic == null || dic.Count == 0) return 0;
var list = Select(whereFunc);
if (list.Count() == 0) return 0;
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase;
Type type = typeof(T);
foreach (var source in list)
{
//设置为未更改
DbContext.Entry(source).State = EntityState.Unchanged;
foreach (var item in dic)
{
PropertyInfo info = type.GetProperty(item.Key, bindingFlags);
if (info != null)
{
info.SetValue(source, item.Value);
DbContext.Entry(source).Property(item.Key).IsModified = true;
}
}
}
DbContext.Configuration.ValidateOnSaveEnabled = false;
return DbContext.SaveChanges();
}
#endregion
}
以上是关于csharp EF增删改查封装的主要内容,如果未能解决你的问题,请参考以下文章