mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 10:37:55 +08:00
同步OpenAuth.Core最新代码
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using OpenAuth.Repository.Core;
|
||||
using OpenAuth.Repository.Interface;
|
||||
using Z.EntityFramework.Plus;
|
||||
@@ -18,6 +20,31 @@ namespace OpenAuth.Repository
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EF默认情况下,每调用一次SaveChanges()都会执行一个单独的事务
|
||||
/// 本接口实现在一个事务中可以多次执行SaveChanges()方法
|
||||
/// </summary>
|
||||
public void ExecuteWithTransaction(Action action)
|
||||
{
|
||||
using (IDbContextTransaction transaction = _context.Database.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回DbContext,用于多线程等极端情况
|
||||
/// </summary>
|
||||
public OpenAuthDBContext GetDbContext()
|
||||
{
|
||||
return _context;
|
||||
@@ -32,7 +59,7 @@ namespace OpenAuth.Repository
|
||||
return Filter(exp);
|
||||
}
|
||||
|
||||
public bool IsExist<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
public bool Any<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
{
|
||||
return _context.Set<T>().Any(exp);
|
||||
}
|
||||
@@ -40,7 +67,7 @@ namespace OpenAuth.Repository
|
||||
/// <summary>
|
||||
/// 查找单个
|
||||
/// </summary>
|
||||
public T FindSingle<T>(Expression<Func<T, bool>> exp) where T:class
|
||||
public T FirstOrDefault<T>(Expression<Func<T, bool>> exp) where T:class
|
||||
{
|
||||
return _context.Set<T>().AsNoTracking().FirstOrDefault(exp);
|
||||
}
|
||||
@@ -63,11 +90,14 @@ namespace OpenAuth.Repository
|
||||
/// <summary>
|
||||
/// 根据过滤条件获取记录数
|
||||
/// </summary>
|
||||
public int GetCount<T>(Expression<Func<T, bool>> exp = null) where T : class
|
||||
public int Count<T>(Expression<Func<T, bool>> exp = null) where T : class
|
||||
{
|
||||
return Filter(exp).Count();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增对象,如果Id为空,则会自动创建默认Id
|
||||
/// </summary>
|
||||
public void Add<T>(T entity) where T : BaseEntity
|
||||
{
|
||||
if (entity.KeyIsNull())
|
||||
@@ -78,9 +108,8 @@ namespace OpenAuth.Repository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量添加
|
||||
/// 批量新增对象,如果对象Id为空,则会自动创建默认Id
|
||||
/// </summary>
|
||||
/// <param name="entities">The entities.</param>
|
||||
public void BatchAdd<T>(T[] entities) where T : BaseEntity
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
@@ -113,18 +142,23 @@ namespace OpenAuth.Repository
|
||||
|
||||
/// <summary>
|
||||
/// 实现按需要只更新部分更新
|
||||
/// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
|
||||
/// <para>如:Update<User>(u =>u.Id==1,u =>new User{Name="ok"})</para>
|
||||
/// <para>该方法内部自动调用了SaveChanges(),需要ExecuteWithTransaction配合才能实现事务控制</para>
|
||||
/// </summary>
|
||||
/// <param name="where">The where.</param>
|
||||
/// <param name="entity">The entity.</param>
|
||||
/// <param name="where">更新条件</param>
|
||||
/// <param name="entity">更新后的实体</param>
|
||||
public void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class
|
||||
{
|
||||
_context.Set<T>().Where(where).Update(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除
|
||||
/// <para>该方法内部自动调用了SaveChanges(),需要ExecuteWithTransaction配合才能实现事务控制</para>
|
||||
/// </summary>
|
||||
public virtual void Delete<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
{
|
||||
_context.Set<T>().RemoveRange(Filter(exp));
|
||||
_context.Set<T>().Where(exp).Delete();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
@@ -178,5 +212,74 @@ namespace OpenAuth.Repository
|
||||
{
|
||||
return _context.Query<T>().FromSqlRaw(sql, parameters);
|
||||
}
|
||||
|
||||
#region 异步实现
|
||||
|
||||
/// <summary>
|
||||
/// 异步执行sql
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> ExecuteSqlRawAsync(string sql)
|
||||
{
|
||||
return await _context.Database.ExecuteSqlRawAsync(sql);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 异步保存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public async Task<int> SaveAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
return await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (ValidationException exc)
|
||||
{
|
||||
Console.WriteLine($"{nameof(Save)} validation exception: {exc?.Message}");
|
||||
throw (exc.InnerException as Exception ?? exc);
|
||||
}
|
||||
catch (Exception ex) //DbUpdateException
|
||||
{
|
||||
throw (ex.InnerException as Exception ?? ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据过滤条件获取记录数
|
||||
/// </summary>
|
||||
public async Task<int> CountAsync<T>(Expression<Func<T, bool>> exp = null) where T : class
|
||||
{
|
||||
return await Filter(exp).CountAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> AnyAsync<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
{
|
||||
return await _context.Set<T>().AnyAsync(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找单个,且不被上下文所跟踪
|
||||
/// </summary>
|
||||
public async Task<T> FirstOrDefaultAsync<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
{
|
||||
return await _context.Set<T>().AsNoTracking().FirstOrDefaultAsync(exp);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user