UserInfoRepository 用户表的数据库访问代码/// <summary>
/// 用户表操作类
/// </summary>
public class UserInfoRepository
{
public UserInfoRepository()
{
}
private ModelFirstDemoEntities db = new ModelFirstDemoEntities();
#region 增加实体
/// <summary>
/// 增加实体
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public UserInfo AddEntity(UserInfo model)
{
db.UserInfo.Add(model);
db.SaveChanges();
return model;
}
#endregion
#region 根据传进来的条件,进行删除操作 int Delete(Expression<Func<UserInfo, bool>> whereLambda)
/// <summary>
/// 根据传进来的条件,进行删除操作 int Delete(Expression<Func<UserInfo, bool>> whereLambda)
/// </summary>
/// <param name="whereLambda">删除的条件</param>
/// <returns></returns>
public int Delete(Expression<Func<UserInfo, bool>> whereLambda)
{
var deleteList = db.UserInfo.Where(whereLambda).ToList();
deleteList.ForEach(u => db.UserInfo.Remove(u));
return db.SaveChanges();
}
#endregion
#region 根据实体需要修改的属性来修改实体的值 int Update(UserInfo model, parameter string[] propertyName)
/// <summary>
/// 根据实体需要修改的属性来修改实体的值 int Update(UserInfo model, string[] propertyName)
/// </summary>
/// <param name="model">修改的实体</param>
/// <param name="propertyName">将要修改的属性</param>
/// <returns>返回影响的行数</returns>
public int Modif(UserInfo model, string[] propertyName)
{
DbEntityEntry entry = db.Entry(model);
entry.State = EntityState.Unchanged; //这里设置传进来要修改的model的状态为 未修改状态
foreach (var proName in propertyName)
{
entry.Property(proName).IsModified = true; //然后针对具体要修改的属性设置为修改
}
return db.SaveChanges();
}
#endregion
#region 批量修改 根据实体需要修改的属性来修改实体的值
/// <summary>
/// 批量修改 根据实体需要修改的属性来修改实体的值
/// 调用实例UserInfo newUserInfo=new UserInfo(){ UserName = "新的名字"};
/// ModifBy(newUserInfo, u => u.Id > 1, "UserName");
/// </summary>
/// <param name="newModel">新的值</param>
/// <param name="whereLambda">查询要修改的数据</param>
/// <param name="propertyName">将要修改的属性</param>
/// <returns>返回影响的行数</returns>
public int ModifBy(UserInfo newModel, Expression<Func<UserInfo, bool>> whereLambda, params string[] propertyName)
{
List<UserInfo> modiList = db.UserInfo.Where(whereLambda).ToList();
Type type = typeof(UserInfo); //获取类型
List<PropertyInfo> propertyInfos= type.GetProperties().ToList();
Dictionary<string,PropertyInfo> dictionary=new Dictionary<string, PropertyInfo>();
foreach (var property in propertyInfos)
{
if (propertyName.Contains(property.Name))
{
dictionary.Add(property.Name,property);
}
}
foreach (var oldUserInfo in modiList)
{
foreach (var proName in propertyName)
{
PropertyInfo property = dictionary[proName];
object val = property.GetValue(newModel, null); //根据属性,来从新的model中 获取数据 比如是获取
property.SetValue(oldUserInfo, val, null); //根据这个属性 来修改值
}
}
return db.SaveChanges();
}
#endregion
#region 查询 不带分页
public List<UserInfo> GetListBy(Expression<Func<UserInfo,bool>> whereLambda)
{
return db.UserInfo.Where(whereLambda).ToList();
}
#endregion
#region 带分页和正序倒序的查询
/// <summary>
/// 带分页和正序倒序的查询
/// </summary>
/// <typeparam name="TKey">这个可以不写,编译器会自动推断你orderby的是哪个列是什么类型(int还是string类型)</typeparam>
/// <param name="whereLambda">查询条件</param>
/// <param name="orderByLambda">排序条件</param>
/// <param name="pagesize">分页大小</param>
/// <param name="pageindex">分页页码</param>
/// <param name="isDEsc">是否倒序,默认是否(也就是说默认我们是用正序从小到大)</param>
/// <returns></returns>
public List<UserInfo> GetListBy<TKey>(Expression<Func<UserInfo, bool>> whereLambda,
Expression<Func<UserInfo, TKey>> orderByLambda, int pagesize = 10, int pageindex = 1, bool isDEsc = false)
{
if (isDEsc)
{
return db.UserInfo.Where(whereLambda).OrderByDescending(orderByLambda)
.Skip(pagesize * (pagesize - 1)).Take(pagesize).ToList();
}
else
{
return db.UserInfo.Where(whereLambda).OrderBy(orderByLambda)
.Skip(pagesize * (pagesize - 1)).Take(pagesize).ToList();
}
}
#endregion
}