内容目录
新建一个静态类吗,注入 dapper 服务。
/// <summary>
/// 注入dapper
/// </summary>
public static class DapperService
{
/// <summary>
/// 注入dapper
/// </summary>
public static IServiceCollection AddDapperContext(this IServiceCollection services, string connectStr)
{
services.AddTransient<IDbConnection, SqliteConnection>(context =>
{
return new SqliteConnection(connectStr);
});
return services;
}
}
Startup.ConfigureServices
方法中,添加
```
使用示例
```c#
public class TestController : ControllerBase
{
private readonly IDbConnection _dapperContext;
///
/// 构造函数
///
///
public TestController(IDbConnection dapperCon)
{
_dapperContext = dapperCon;
}
文章评论