mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 02:29:24 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
@@ -1,21 +1,26 @@
|
||||
using System;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Migrations;
|
||||
using System.Data.Entity.Validation;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using EntityFramework.Extensions;
|
||||
using Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OpenAuth.Repository.Core;
|
||||
using OpenAuth.Repository.Interface;
|
||||
using Z.EntityFramework.Plus;
|
||||
|
||||
namespace OpenAuth.Repository
|
||||
{
|
||||
public class BaseRepository<T> :IRepository<T> where T :Domain.Entity
|
||||
{
|
||||
protected OpenAuthDBContext Context = new OpenAuthDBContext();
|
||||
public class BaseRepository<T> : IRepository<T> where T : BaseEntity
|
||||
{
|
||||
private OpenAuthDBContext _context;
|
||||
|
||||
public BaseRepository(OpenAuthDBContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 根据过滤条件,获取记录
|
||||
/// </summary>
|
||||
/// <param name="exp">The exp.</param>
|
||||
@@ -26,7 +31,7 @@ namespace OpenAuth.Repository
|
||||
|
||||
public bool IsExist(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return Context.Set<T>().Any(exp);
|
||||
return _context.Set<T>().Any(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -34,7 +39,7 @@ namespace OpenAuth.Repository
|
||||
/// </summary>
|
||||
public T FindSingle(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
return Context.Set<T>().AsNoTracking().FirstOrDefault(exp);
|
||||
return _context.Set<T>().AsNoTracking().FirstOrDefault(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -43,7 +48,8 @@ namespace OpenAuth.Repository
|
||||
/// <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)
|
||||
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))
|
||||
@@ -62,12 +68,14 @@ namespace OpenAuth.Repository
|
||||
|
||||
public void Add(T entity)
|
||||
{
|
||||
if (string.IsNullOrEmpty(entity.Id))
|
||||
{
|
||||
entity.Id = Guid.NewGuid().ToString();
|
||||
if (entity.KeyIsNull())
|
||||
{
|
||||
entity.GenerateDefaultKeyVal();
|
||||
}
|
||||
Context.Set<T>().Add(entity);
|
||||
|
||||
_context.Set<T>().Add(entity);
|
||||
Save();
|
||||
_context.Entry(entity).State = EntityState.Detached;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -78,19 +86,23 @@ namespace OpenAuth.Repository
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
entity.Id = Guid.NewGuid().ToString();
|
||||
if (entity.KeyIsNull())
|
||||
{
|
||||
entity.GenerateDefaultKeyVal();
|
||||
}
|
||||
}
|
||||
Context.Set<T>().AddRange(entities);
|
||||
|
||||
_context.Set<T>().AddRange(entities);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Update(T entity)
|
||||
{
|
||||
var entry = this.Context.Entry(entity);
|
||||
var entry = this._context.Entry(entity);
|
||||
entry.State = EntityState.Modified;
|
||||
|
||||
//如果数据没有发生变化
|
||||
if (!this.Context.ChangeTracker.HasChanges())
|
||||
if (!this._context.ChangeTracker.HasChanges())
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -100,20 +112,10 @@ namespace OpenAuth.Repository
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
Context.Set<T>().Remove(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>
|
||||
/// 实现按需要只更新部分更新
|
||||
@@ -123,37 +125,73 @@ namespace OpenAuth.Repository
|
||||
/// <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);
|
||||
_context.Set<T>().Where(where).Update(entity);
|
||||
}
|
||||
|
||||
public virtual void Delete(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
Context.Set<T>().Where(exp).Delete();
|
||||
_context.Set<T>().Where(exp).Delete();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
Context.SaveChanges();
|
||||
var entities = _context.ChangeTracker.Entries()
|
||||
.Where(e => e.State == EntityState.Added
|
||||
|| e.State == EntityState.Modified)
|
||||
.Select(e => e.Entity);
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var validationContext = new ValidationContext(entity);
|
||||
Validator.ValidateObject(entity, validationContext, validateAllProperties: true);
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
}
|
||||
catch (DbEntityValidationException e)
|
||||
catch (ValidationException exc)
|
||||
{
|
||||
throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
|
||||
Console.WriteLine($"{nameof(Save)} validation exception: {exc?.Message}");
|
||||
throw (exc.InnerException as Exception ?? exc);
|
||||
}
|
||||
catch (Exception ex) //DbUpdateException
|
||||
{
|
||||
throw (ex.InnerException as Exception ?? ex);
|
||||
}
|
||||
}
|
||||
|
||||
private IQueryable<T> Filter(Expression<Func<T, bool>> exp)
|
||||
{
|
||||
var dbSet = Context.Set<T>().AsNoTracking().AsQueryable();
|
||||
var dbSet = _context.Set<T>().AsNoTracking().AsQueryable();
|
||||
if (exp != null)
|
||||
dbSet = dbSet.Where(exp);
|
||||
return dbSet;
|
||||
}
|
||||
|
||||
public int ExecuteSql(string sql)
|
||||
{
|
||||
return Context.Database.ExecuteSqlCommand(sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
public int ExecuteSql(string sql)
|
||||
{
|
||||
return _context.Database.ExecuteSqlRaw(sql);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用SQL脚本查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"> T为数据库实体</typeparam>
|
||||
/// <returns></returns>
|
||||
public IQueryable<T> FromSql(string sql, params object[] parameters)
|
||||
{
|
||||
return _context.Set<T>().FromSqlRaw(sql, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用SQL脚本查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"> T为非数据库实体,需要在DbContext中增加对应的DbQuery</typeparam>
|
||||
/// <returns></returns>
|
||||
public IQueryable<T> Query(string sql, params object[] parameters)
|
||||
{
|
||||
return _context.Query<T>().FromSqlRaw(sql, parameters);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user