You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

45 lines
1.5 KiB

using FreeSql;
using Microsoft.Extensions.DependencyInjection;
using Znyc.CloudCar.Auth.HttpContextUser;
using Znyc.CloudCar.Configuration;
using Znyc.CloudCar.Core.Db;
namespace Znyc.CloudCar.Core.Config
{
/// <summary>
/// FreeSql
/// </summary>
public static class FreeSqlSetup
{
public static void AddFreeSqlSetup(this IServiceCollection services)
{
var freeSqlBuiler = new FreeSqlBuilder()
.UseConnectionString(DataType.MySql, AppSettingsConstVars.DbSqlConnection)
.UseLazyLoading(false)
.UseNoneCommandParameter(true);
//监听所有命令
if (AppSettingsConstVars.SqlMonitorCommand)
{
freeSqlBuiler.UseMonitorCommand(cmd => { },
(cmd, traceLog) => { Console.WriteLine($"{cmd.CommandText}\r\n"); });
}
var fsql = freeSqlBuiler.Build();
services.AddScoped<UnitOfWorkManager>();
services.AddSingleton(fsql);
//监听所有sql语句
if (AppSettingsConstVars.SqlCurd)
{
fsql.Aop.CurdAfter += (s, e) =>
{
Console.WriteLine($"{e.Sql}\r\n");
Console.WriteLine($"耗时:{e.ElapsedMilliseconds}ms\r\n");
};
}
//审计数据
var user = services.BuildServiceProvider().GetService<IHttpContextUser>();
fsql.Aop.AuditValue += (s, e) => { DbHelper.AuditValue(e, user); };
}
}
}