Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Core Connection String Dapper visual studio 2017

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

System.NullReferenceException: 'Object reference not set to an instance of an object.'

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;"
}

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;"/>

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: 'Object reference not set to an instance of an object.'

I have used the following resources:

.Net CORE Dapper Connection String?

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

Get connection string from App.config

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.

like image 816
JoeG Avatar asked Sep 13 '17 01:09

JoeG


People also ask

Where are connection strings stored in ASP NET Core?

In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings.json, an environment variable, the user secret store, or another configuration source. See the Configuration section of the ASP.NET Core documentation for more details.

How to set default connection string for Dapper PS?

Use phase Configuration.GetConnectionString("DefaultConnection") You can use this value for Dapper P.S. You need to add 3 dependencies into your project.json

How to add business logic project in ASP NET Core framework?

Because in ASP.NET Core Framework, ASP.NET MVC & ASP.NET Web API has been merged and both have the parent controller as Controller (Microsoft.AspNetCore.Mvc.Controller). Add Business logic project for Web API. Right click on Solution Explorer -> Add -> New Project.

How do I add a connection string to my application?

The connection string should be added to your application's App.config file (Web.config if you are using ASP.NET). If your connection string contains sensitive information, such as username and password, you can protect the contents of the configuration file using the Secret Manager tool.


1 Answers

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;}
}

Add this line in ConfigureServices method in Startup.cs

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

Data service class

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;
   }
}

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(); 
     }
 }
like image 101
Yared Avatar answered Sep 19 '22 03:09

Yared