using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Linq.Expressions;
using EntityFramework.Extensions;
using OpenAuth.Domain.Interface;
using Infrastructure;
using OpenAuth.Domain;
using OpenAuth.Repository.Models;
namespace OpenAuth.Repository
{
public class UnitWork: IUnitWork
{
protected OpenAuthDBContext Context = new OpenAuthDBContext();
///
/// 根据过滤条件,获取记录
///
/// The exp.
public IQueryable Find(Expression> exp = null) where T : class
{
return Filter(exp);
}
public bool IsExist(Expression> exp) where T : class
{
return Context.Set().Any(exp);
}
///
/// 查找单个
///
public T FindSingle(Expression> exp) where T:class
{
return Context.Set().AsNoTracking().FirstOrDefault(exp);
}
///
/// 得到分页记录
///
/// The pageindex.
/// The pagesize.
/// 排序,格式如:"Id"/"Id descending"
public IQueryable Find(int pageindex, int pagesize, string orderby = "", Expression> exp = null) where T : class
{
if (pageindex < 1) pageindex = 1;
if (string.IsNullOrEmpty(orderby))
orderby = "Id descending";
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
}
///
/// 根据过滤条件获取记录数
///
public int GetCount(Expression> exp = null) where T : class
{
return Filter(exp).Count();
}
public void Add(T entity) where T : Domain.Entity
{
entity.Id = Guid.NewGuid().ToString();
Context.Set().Add(entity);
}
///
/// 批量添加
///
/// The entities.
public void BatchAdd(T[] entities) where T : Domain.Entity
{
foreach (var entity in entities)
{
entity.Id = Guid.NewGuid().ToString();
}
Context.Set().AddRange(entities);
}
public void Update(T entity) where T:class
{
var entry = this.Context.Entry(entity);
//todo:如果状态没有任何更改,会报错
entry.State = EntityState.Modified;
}
public void Delete(T entity) where T:class
{
Context.Set().Remove(entity);
}
///
/// 按指定id更新实体,会更新整个实体
///
/// The identity exp.
/// The entity.
public void Update(Expression> identityExp, T entity) where T:class
{
Context.Set().AddOrUpdate(identityExp, entity);
}
///
/// 实现按需要只更新部分更新
/// 如:Update(u =>u.Id==1,u =>new User{Name="ok"});
///
/// The where.
/// The entity.
public void Update(Expression> where, Expression> entity) where T:class
{
Context.Set().Where(where).Update(entity);
}
public virtual void Delete(Expression> exp) where T : class
{
Context.Set().Where(exp).Delete();
}
public void Save()
{
try
{
Context.SaveChanges();
}
catch (DbEntityValidationException e)
{
throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
}
}
private IQueryable Filter(Expression> exp) where T : class
{
var dbSet = Context.Set().AsQueryable();
if (exp != null)
dbSet = dbSet.Where(exp);
return dbSet;
}
}
}