mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-12-27 06:35:39 +08:00
fix #I3O97D 站点启动时自动运行状态为【正在运行】的定时任务;
fix #I3ODHI 增加存储过程调用;
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
@@ -107,6 +109,13 @@ namespace OpenAuth.Repository.Interface
|
||||
/// <returns></returns>
|
||||
IQueryable<T> Query<T>(string sql, params object[] parameters) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程
|
||||
/// </summary>
|
||||
/// <param name="procName">存储过程名称</param>
|
||||
/// <param name="sqlParams">存储过程参数</param>
|
||||
List<T> ExecProcedure<T>(string procName,params DbParameter[] sqlParams) where T : class;
|
||||
|
||||
#region 异步接口
|
||||
|
||||
Task<int> ExecuteSqlRawAsync(string sql);
|
||||
|
||||
@@ -21,16 +21,14 @@ namespace OpenAuth.Repository
|
||||
private ILoggerFactory _LoggerFactory;
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
private IConfiguration _configuration;
|
||||
private IOptions<AppSetting> _appConfiguration;
|
||||
|
||||
public OpenAuthDBContext(DbContextOptions<OpenAuthDBContext> options, ILoggerFactory loggerFactory,
|
||||
IHttpContextAccessor httpContextAccessor, IConfiguration configuration, IOptions<AppSetting> appConfiguration)
|
||||
IHttpContextAccessor httpContextAccessor, IConfiguration configuration)
|
||||
: base(options)
|
||||
{
|
||||
_LoggerFactory = loggerFactory;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_configuration = configuration;
|
||||
_appConfiguration = appConfiguration;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
|
||||
28
OpenAuth.Repository/Test/TestUnitWork.cs
Normal file
28
OpenAuth.Repository/Test/TestUnitWork.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using NUnit.Framework;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.Repository.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// 测试UnitWork
|
||||
/// </summary>
|
||||
class TestUnitWork : TestBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 测试存储过程
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ExecProcedure()
|
||||
{
|
||||
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
|
||||
var users = unitWork.ExecProcedure<User>("sp_alluser");
|
||||
Console.WriteLine(JsonHelper.Instance.Serialize(users));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using OpenAuth.Repository.Core;
|
||||
@@ -212,8 +216,29 @@ namespace OpenAuth.Repository
|
||||
{
|
||||
return _context.Query<T>().FromSqlRaw(sql, parameters);
|
||||
}
|
||||
|
||||
#region 异步实现
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程
|
||||
/// </summary>
|
||||
/// <param name="procName">存储过程名称</param>
|
||||
/// <param name="sqlParams">存储过程参数</param>
|
||||
public List<T> ExecProcedure<T>(string procName, params DbParameter[] sqlParams) where T : class
|
||||
{
|
||||
var connection = _context.Database.GetDbConnection();
|
||||
using (var cmd = connection.CreateCommand())
|
||||
{
|
||||
_context.Database.OpenConnection();
|
||||
cmd.CommandText = procName;
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
cmd.Parameters.AddRange(sqlParams);
|
||||
DbDataReader dr = cmd.ExecuteReader();
|
||||
var datatable = new DataTable();
|
||||
datatable.Load(dr);
|
||||
return datatable.ToList<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#region 异步实现
|
||||
|
||||
/// <summary>
|
||||
/// 异步执行sql
|
||||
|
||||
Reference in New Issue
Block a user