Net Core Connection String Dapper visual studio 2017

Viewed 7736

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.

1 Answers
Related