Net Core Connection String Dapper Visual Studio 2017 [英] Net Core Connection String Dapper visual studio 2017

查看:22
本文介绍了Net Core Connection String Dapper Visual Studio 2017的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 .Net Core 应用程序中设置连接字符串,但我不断收到错误消息:

I am trying to get a Connection String set up in my .Net Core application but i keep getting the error:

System.NullReferenceException:'未将对象引用设置为对象的实例.'

我尝试将以下内容添加到 appsettings.json:

I have tried adding the following to appsettings.json:

"ConnectionStrings": {

"Analysis": "Server=DESKTOP-MYSERVER;Database=MYDATABASE;User Id=sa; Password=Password123;Provider=System.Data.SqlClient;Trusted_Connection=True;MultipleActiveResultSets=true;Pooling=false;"
}

我也尝试使用 web.config,就像我在使用 .Net Core 之前一样:

I also tried using web.config like I used to before .Net Core:

<connectionStrings>
<add name="Analysis" providerName="System.Data.SqlClient" 
     connectionString="Server=DESKTOP-MYSERVER;Database=MYDATABASE;User Id=sm;Password=Password123;"/>

然后在 c# 中我有:

Then in c# i have:

public List<DapperTest> ReadAll()
    {
        var data = new List<DapperTest>();
        using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["Analysis"].ConnectionString))
        {
             data = db.Query<DapperTest>("select * from testTable").ToList();
        }

        return data;
    }

两种方式都给我一个例外:

Both ways give me the exception of:

System.NullReferenceException:'未将对象引用设置为对象的实例.'

我使用了以下资源:

.Net CORE Dapper 连接字符串?

https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

从 App.config 获取连接字符串

但是我遗漏了一些东西.我只设置了一次连接字符串,它不在 .Net Core 中,因此对其他人来说可能很明显.

But I am missing something. I have only set up connection strings once and it was not in .Net Core so it could be obvious to others.

推荐答案

如果您使用的是 appsettings.json,请创建一个简单的 POCO 类来为您的连接字符串配置建模,如下所示:

If you are using appsettings.json, create a simple POCO class to model your connection string configurations like this:

public class ConnectionConfig
{
        public string Analysis {get;set;}
}

在 Startup.cs 的 ConfigureServices 方法中添加这一行

Add this line in ConfigureServices method in Startup.cs

services.Configure<ConnectionConfig>(Configuration.GetSection("ConnectionStrings"));

数据服务类

class YourClass{
    private string _connectionString;

    YourClass(string connectionString){
       _connectionString = connectionString;
    }

    //Your implementation
    public List<DapperTest> ReadAll()
    {
        var data = new List<DapperTest>();
        using (IDbConnection db = new SqlConnection(_connectionString)
        {
            data = db.Query<DapperTest>("select * from testTable").ToList();
        }

       return data;
   }
}

在您的控制器构造函数中注入 IOptions.

Inject IOptions<ConnectionConfig> in your controller constructor .

class YourController : Controller{
   YourClass _testing;

   YourController(IOptions<ConnectionConfig> connectionConfig){
       var connection = connectionConfig.Value;
       string connectionString = connection.Analysis;
       _testing = new YourClass(connectionString );
    }
   public IActionResult Index() { 
        var testingData = _testing.ReadAll(); 
        return View(); 
     }
 }

这篇关于Net Core Connection String Dapper Visual Studio 2017的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆