.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. 学习笔记《简明python教程》

    学习笔记<简明python教程> 体会:言简意赅,很适合新手入门 2018年3月14日21:45:59 1.global 语句 在不使用 global 语句的情况下,不可能为一个定义于函数 ...

  2. python 之字符编码

    一    了解字符编码的储备知识 python解释器和文件本编辑的异同      相同点:python解释器是解释执行文件内容的,因而python解释器具备读py文件的功能,这一点与文本编辑器一样 不 ...

  3. 现代编译原理--第六章(中间树 IR Tree 含源码)

    (转载请表明出处   http://www.cnblogs.com/BlackWalnut/p/4559717.html ) 这一章,就虎书而言,理论知识点是及其少的,就介绍了为什么要有一个中间表示树 ...

  4. AutoCAD开发2--添加带属性的点

    Private Sub CommandButton11_Click() Dim pPoint As AcadPoint Dim DataType(0 To 1) As Integer Dim Data ...

  5. linux安装mysql和httpd

    1.安装前检查是否已经安装[root@localhost1 ~]# rpm -qa |grep mysql 2.安装wget包:[root@localhost1 ~]# yum -y install ...

  6. Collision (hdu-5114

    题意:你有一个以(0, 0), (x, 0), (0, y), (x, y)为边界点的四边形,给你两个点从(x1, y1), (x2, y2)的点发射,以(1, 1)的速度走,碰到边界会反射,问你最终 ...

  7. SAS对数据变量的处理

    SAS对数据变量的处理 在使用DATA步基于已经存在的数据集生成新数据集时,可以指定在新数据集中不需要包含的变量而仅读取其他变量,或者指定仅需要在 新数据集中包含的变量.该功能可以通过DATA步中的S ...

  8. 2019swpuj2ee作业一:C/S,B/S的应用的区别

    1.硬件环境不同: C/S 一般建立在专用的网络上, 小范围里的网络环境, 局域网之间再通过专门服务器提供连接和数据交换服务.B/S 建立在广域网之上的, 不必是专门的网络硬件环境,例与电话上网,  ...

  9. Android -Services 使用简介

    Android Services 四大组件之一,主要用于后台长时间运行.没有界面.这里讲解两种services的启动还有AIDL通信方式. 1.startservices a.建立继承services ...

  10. Vuejs——(11)组件——slot内容分发

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...