.Net Core中一个特别重要的特性就是依赖注入功能,那么我们在使用PetaPoco的时候是否也可以使用依赖注入特性呢?

回答当然是可以的啦。使用方法(两种注入方式)如下

services.AddScoped<IDatabase>(
x =>
{
var connectionStrnig = Configuration["ConnectionStrings:MySQL:MvcMySQL"];
var configuration = DatabaseConfiguration.Build().UsingConnectionString(connectionStrnig)
.UsingProvider<MariaDbDatabaseProvider>();
return new PetaPocoMvcDBContext(configuration);
}); services.AddScoped<IDatabase, PetaPocoMvcDBContext>(
(x) =>
{
var connectionStrnig = Configuration["ConnectionStrings:MySQL:MvcMySQL"];
var configuration = DatabaseConfiguration.Build().UsingConnectionString(connectionStrnig)
.UsingProvider<MariaDbDatabaseProvider>();
return new PetaPocoMvcDBContext(configuration);
});

定义的PetaPocoMvcDBContext类:

namespace PetaPocoEfCoreMvc.DBContext
{
using System.Data.Common; using PetaPoco;
using PetaPoco.Core; public class PetaPocoMvcDBContext:Database
{
public PetaPocoMvcDBContext(DbConnection connection, IMapper defaultMapper = null)
: base(connection, defaultMapper)
{
} public PetaPocoMvcDBContext(string connectionString, string providerName, IMapper defaultMapper = null)
: base(connectionString, providerName, defaultMapper)
{
} public PetaPocoMvcDBContext(string connectionString, DbProviderFactory factory, IMapper defaultMapper = null)
: base(connectionString, factory, defaultMapper)
{
} public PetaPocoMvcDBContext(string connectionString, IProvider provider, IMapper defaultMapper = null)
: base(connectionString, provider, defaultMapper)
{
} public PetaPocoMvcDBContext(IDatabaseBuildConfiguration configuration)
: base(configuration)
{
}
}
}

appsetting.json中的数据库连接字符串:

"ConnectionStrings": {
"MySQL": {
"MvcMySQL": "server=127.0.0.1;port=3306;uid=root;pwd=123456;database=WireCloud;",
"provider": "MySql.Data.MySqlClient"
}
}

添加UserService相关类:

namespace PetaPocoEfCoreMvc.Service
{
using PetaPocoEfCoreMvc.Models; public interface IUserService
{
IEnumerable<User> GetAll();
}
} namespace PetaPocoEfCoreMvc.Service
{
using PetaPoco;
using PetaPocoEfCoreMvc.Models; public class UserService:IUserService
{
private readonly IDatabase _database;
    //构造函数注入
public UserService(IDatabase database)
{
_database = database;
} public IEnumerable<User> GetAll()
{
return _database.Fetch<User>();
}
}
}

PetaPoco在ASP.NET Core 2.2中使用注入方式访问数据库的更多相关文章

  1. ASP.NET Core HTTP 管道中的那些事儿

    前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Core 中间件系列的,但是中间遇到了很多事情.首先是 NPOI ...

  2. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  3. 在ASP.NET Core 1.0中如何发送邮件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...

  4. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  5. 用ASP.NET Core 1.0中实现邮件发送功能

    准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...

  6. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  7. 基础教程:视图中的ASP.NET Core 2.0 MVC依赖注入

    问题 如何在ASP.NET Core MVC Views中注入和使用服务. 解 更新 启动 类来为MVC添加服务和中间件. 添加一项服务 添加一个Controller,返回 ViewResult. 添 ...

  8. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  9. 使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目

    今天,Visual Studio中没有内置工具来测试WEB API.使用浏览器,只能测试http GET请求.您需要使用Postman,SoapUI,Fiddler或Swagger等第三方工具来执行W ...

随机推荐

  1. 用户认证--auth模块实现

    转载文章,如有不妥之处请谅解 相关介绍 auth auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. auth可以和admin模块配合使用, 快速建立网站的管 ...

  2. 深入理解 requestAnimationFrame

    在Web应用中,实现动画效果的方法比较多,Javascript 中可以通过定时器 setTimeout 来实现,css3 可以使用 transition 和 animation 来实现,html5 中 ...

  3. hMailServer安装使用教程

    hMialServer是Windows下一款免费开源的邮件服务器软件,支持smtp.pop3.imap. 本文主要根据官方文档Quick-Start guide整理而成. 一.下载 下载地址:http ...

  4. CSU 1684-Disastrous Downtime

    题目链接:https://nanti.jisuanke.com/t/28879 思路:贪心,从最早收到请求的时刻开始,统计每个相差1000毫秒的时间段内接收的请求数量再计算该时间段内所需机器数目,答案 ...

  5. python_paramiko

    目录: paramiko模块介绍 paramiko模块安装 paramiko模块使用 一.paramiko模块介绍 paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件 ...

  6. 别人的Linux私房菜(10)vim程序编辑器

    很多软件的编辑接口会主动调用vi vi分一般命令模式.编辑模式.命令行模式. 使用vi :/bin/vi welcome.txt 下下端显示文本有多少行,多少字符, 一般命令模式: 上下左右移动光标k ...

  7. VIP之Switch

    Switch II 最大能连接12路输入与12路输出 不能合并数据数 每个输入可以驱动多个输出 每个输出只能被一个输入驱动 当输入没有连接到输出时,可以禁止掉 每个被禁止的输入可以设置成停止或者消耗模 ...

  8. Django的一些隐性经验

    隐性经验 前后信息的沟通 url中的参数 get获取 这个参数可以写在URL当中(可以写多个,写在这里的get函数需要有相应的参数去获取).,也可以在模版中添加(通过?若是直接写则表示在当前的URL中 ...

  9. Django View 进阶

    返回404 from django.http import HttpResponse, HttpResponseNotFound def not_found(request): ) 或 return ...

  10. Learning WCF:Fault Handling

    There are two types of Execptions which can be throwed from the WCF service. They are Application ex ...