.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. golang环境 centos 7

    https://blog.csdn.net/ggq89/article/details/82682171  Linux下Go的安装.配置 .升级和卸载 https://blog.csdn.net/we ...

  2. 服务器重新启动,ftp重新连接问题

    服务器重新启动,发现FlashFXP无法连接了,估计是ftp没有启动, 1. 首先服务器要安装ftp软件,查看是否已经安装ftp软件下:   #which vsftpd   如果看到有vsftpd的目 ...

  3. [翻译]高并发框架 LMAX Disruptor 介绍

    原文地址:Concurrency with LMAX Disruptor – An Introduction 译者序 前些天在并发编程网,看到了关于 Disruptor 的介绍.感觉此框架惊为天人,值 ...

  4. Python:每日一题003

    题目: 一个整数,它加上100和加上268后都是一个完全平方数,请问该数是多少? 程序分析: 在10000以内判断,将该数加上100后再开方,加上268后再开方,如果开方后的结果满足如下条件,即是结果 ...

  5. c++ 计算cpu占用率

    计算CPU占用率就是获取系统总的内核时间 用户时间及空闲时间 其中空闲时间就是内核空转 所以内核时间包含空闲时间 然后计算 运行时间 = 内核时间 加 用户时间 减去 空闲时间 间隔时间 =  内核时 ...

  6. Linux学习笔记:Tomcat安装与使用

    Tomcat是一个Servlet容器服务器,用java实现的. 目录结构 Tomcat的安装也很简单,从官网下载安装包.解压后的tomcat目录 apache-tomcat-7.0.92 下的子目录为 ...

  7. 用python语言算π值并且带有进度条

    用python算圆周率π 1.准备第三方库pip 打开cmd 输入代码:pip install requests ,随后就会成功 因为小编已经安装好了,所以就不把图截出来了 2.利用马青公式求π    ...

  8. ABP框架系列之二十一:(Domain-Services-领域服务)

    Introduction Domain Services (or just Service, in DDD) is used to perform domain operations and busi ...

  9. python模块:xml.etree.ElementTree

    """Lightweight XML support for Python. XML is an inherently hierarchical data format, ...

  10. 判断终端类型、微信的文章防盗链、h5页面跳转打开新的app、跳转到app市场

    判断终端的类型.安卓.ios.微信.qq function  GetMobelType()  {                 var  browser  =   {                 ...