mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 02:29:24 +08:00
采用全新的数据库架构
This commit is contained in:
@@ -1,13 +1,86 @@
|
||||
using OpenAuth.Repository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenAuth.Repository
|
||||
{
|
||||
public class BaseRepository
|
||||
{
|
||||
protected OpenAuthDBContext Context = new OpenAuthDBContext();
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Data.Entity.Migrations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using OpenAuth.Domain.Interface;
|
||||
using OpenAuth.Repository.Models;
|
||||
using Infrastructure;
|
||||
|
||||
namespace OpenAuth.Repository
|
||||
{
|
||||
public class BaseRepository<T> :IRepository<T> where T :class
|
||||
{
|
||||
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>().FirstOrDefault(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";
|
||||
|
||||
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据过滤条件获取记录数
|
||||
/// </summary>
|
||||
public int GetCount(Expression<Func<T, bool>> exp = null)
|
||||
{
|
||||
return Filter(exp).Count();
|
||||
}
|
||||
|
||||
public void Add(T entity)
|
||||
{
|
||||
Context.Set<T>().Add(entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Update(T entity)
|
||||
{
|
||||
Context.Set<T>().AddOrUpdate(entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
Context.Set<T>().Remove(entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
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