mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-18 17:48:01 +08:00
增加mysql支持
This commit is contained in:
@@ -14,91 +14,122 @@ namespace OpenAuth.Repository
|
||||
{
|
||||
protected OpenAuthDBContext Context = new OpenAuthDBContext();
|
||||
|
||||
/// <summary>
|
||||
/// 根据过滤条件,获取记录
|
||||
/// </summary>
|
||||
/// <param name="exp">The exp.</param>
|
||||
public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)
|
||||
{
|
||||
return Filter(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找单个
|
||||
/// </summary>
|
||||
public T FindSingle(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return Context.Set<T>().AsNoTracking().FirstOrDefault(exp);
|
||||
}
|
||||
/// 根据过滤条件,获取记录
|
||||
/// </summary>
|
||||
/// <param name="exp">The exp.</param>
|
||||
public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)
|
||||
{
|
||||
return Filter(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到分页记录
|
||||
/// </summary>
|
||||
/// <param name="pageindex">The pageindex.</param>
|
||||
/// <param name="pagesize">The pagesize.</param>
|
||||
/// <param name="orderby">排序,格式如:"Id"/"Id descending"</param>
|
||||
public IQueryable<T> Find(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null)
|
||||
{
|
||||
if (pageindex < 1) pageindex = 1;
|
||||
if (string.IsNullOrEmpty(orderby))
|
||||
orderby = "Id descending";
|
||||
public bool IsExist(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return Context.Set<T>().Any(exp);
|
||||
}
|
||||
|
||||
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
|
||||
}
|
||||
/// <summary>
|
||||
/// 查找单个
|
||||
/// </summary>
|
||||
public T FindSingle(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return Context.Set<T>().AsNoTracking().FirstOrDefault(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据过滤条件获取记录数
|
||||
/// </summary>
|
||||
public int GetCount(Expression<Func<T, bool>> exp = null)
|
||||
{
|
||||
return Filter(exp).Count();
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到分页记录
|
||||
/// </summary>
|
||||
/// <param name="pageindex">The pageindex.</param>
|
||||
/// <param name="pagesize">The pagesize.</param>
|
||||
/// <param name="orderby">排序,格式如:"Id"/"Id descending"</param>
|
||||
public IQueryable<T> Find(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null)
|
||||
{
|
||||
if (pageindex < 1) pageindex = 1;
|
||||
if (string.IsNullOrEmpty(orderby))
|
||||
orderby = "Id descending";
|
||||
|
||||
public void Add(T entity)
|
||||
{
|
||||
Context.Set<T>().Add(entity);
|
||||
Save();
|
||||
}
|
||||
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
|
||||
}
|
||||
|
||||
public void Update(T entity)
|
||||
{
|
||||
var entry = this.Context.Entry(entity);
|
||||
//todo:如果状态没有任何更改,会报错
|
||||
entry.State = EntityState.Modified;
|
||||
/// <summary>
|
||||
/// 根据过滤条件获取记录数
|
||||
/// </summary>
|
||||
public int GetCount(Expression<Func<T, bool>> exp = null)
|
||||
{
|
||||
return Filter(exp).Count();
|
||||
}
|
||||
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
Context.Set<T>().Remove(entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Update(Expression<Func<T, object>> identityExp, T entity)
|
||||
{
|
||||
//TODO: 暂时有问题,EntityFramework.Extension的Update必须有new操作
|
||||
// Context.Set<T>().Where(exp).Update(u => entity);
|
||||
Context.Set<T>().AddOrUpdate(identityExp, entity);
|
||||
public void Add(T entity)
|
||||
{
|
||||
Context.Set<T>().Add(entity);
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
Context.Set<T>().Where(exp).Delete();
|
||||
}
|
||||
/// <summary>
|
||||
/// 批量添加
|
||||
/// </summary>
|
||||
/// <param name="entities">The entities.</param>
|
||||
public void BatchAdd(T[] entities)
|
||||
{
|
||||
Context.Set<T>().AddRange(entities);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Context.SaveChanges();
|
||||
}
|
||||
public void Update(T entity)
|
||||
{
|
||||
var entry = this.Context.Entry(entity);
|
||||
//todo:如果状态没有任何更改,会报错
|
||||
entry.State = EntityState.Modified;
|
||||
|
||||
private IQueryable<T> Filter(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
var dbSet = Context.Set<T>().AsQueryable();
|
||||
if (exp != null)
|
||||
dbSet = dbSet.Where(exp);
|
||||
return dbSet;
|
||||
}
|
||||
}
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
Context.Set<T>().Remove(entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按指定id更新实体,会更新整个实体
|
||||
/// </summary>
|
||||
/// <param name="identityExp">The identity exp.</param>
|
||||
/// <param name="entity">The entity.</param>
|
||||
public void Update(Expression<Func<T, object>> identityExp, T entity)
|
||||
{
|
||||
Context.Set<T>().AddOrUpdate(identityExp, entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实现按需要只更新部分更新
|
||||
/// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
|
||||
/// </summary>
|
||||
/// <param name="where">The where.</param>
|
||||
/// <param name="entity">The entity.</param>
|
||||
public void Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity)
|
||||
{
|
||||
Context.Set<T>().Where(where).Update(entity);
|
||||
}
|
||||
|
||||
public virtual void Delete(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
Context.Set<T>().Where(exp).Delete();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Context.SaveChanges();
|
||||
}
|
||||
|
||||
private IQueryable<T> Filter(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
var dbSet = Context.Set<T>().AsQueryable();
|
||||
if (exp != null)
|
||||
dbSet = dbSet.Where(exp);
|
||||
return dbSet;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user