BaseDAL

public class BaseDAL<T> where T:class
{
/// <summary>
/// 数据上下文(所有操作基于model)
/// </summary>
public Model1 model
{
get { return new Model1(); }
}
/// <summary>
/// 查询集合
/// </summary>
/// <param name="Expression">表达式</param>
/// <returns>List集合</returns>
public List<T> Select(Expression<Func<T,bool>>Expression)
{
return model.Set<T>().Where(Expression).ToList();
}

/// <summary>
/// 查询单个对象
/// </summary>
/// <param name="Expression">表达式</param>
/// <returns>单个对象或null</returns>
public T Single(Expression<Func<T, bool>> Expression)
{
return model.Set<T>().FirstOrDefault(Expression);
}
/// <summary>
/// 查询单个对象
/// </summary>
/// <param name="id">主键</param>
/// <returns>单个对象或null</returns>
public T Single(int id)
{
return model.Set<T>().Find(id);
}
/// <summary>
/// 添加
/// </summary>
/// <param name="info">数据</param>
/// <returns>受影响的行数</returns>
public int Insert(T info)
{
model.Set<T>().Add(info);
return model.SaveChanges();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="info">数据</param>
/// <returns>受影响的行数</returns>
public int Delete(T info)
{
model.Set<T>().Remove(info);
return model.SaveChanges();
}
}

posted @ 2021-11-24 13:54  壹块钱!  阅读(30)  评论(0编辑  收藏  举报