引入sqlsugar 返回
SqlSugar
沟通中
1
110
waxsq 发布于2周前
悬赏:0 飞吻
public static class SqlSugarConfig
{
public static IServiceCollection AddSqlSugarClient(this IServiceCollection services,IConfiguration configuration)
{
Console.WriteLine("hello");
services.AddScoped<ISqlSugarClient>(s =>
{
//Scoped用SqlSugarClient
SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = configuration.GetConnectionString("MysqlConn"),
IsAutoCloseConnection = true,
},
db =>
{
//每次上下文都会执行
Console.WriteLine("hello world");
//获取IOC对象不要求在一个上下文
//var log=s.GetService<Log>()
//获取IOC对象要求在一个上下文
var appServive = s.GetService<IHttpContextAccessor>();
var log = appServive?.HttpContext?.RequestServices.GetService<ISqlSugarClient>();
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine($"SQL: {sql}");
if (pars != null && pars.Length > 0)
{
Console.WriteLine("参数:");
foreach (var param in pars)
{
Console.WriteLine($" {param.ParameterName}: {param.Value}");
}
}
};
});
return sqlSugar;
});
return services;
}
}public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();
builder.Services.AddSqlSugarClient(builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI();
app.MapControllers();
app.Run();
}
}为什么引入不了sqlsugar的?有大佬帮我解决一下吗
热忱回答(1)
-
fate sta VIP0
2周前你这儿只有注入代码,并没有IOC调用代码。
0 回复