I'm battling to find resources on how to setup a database connection string in ASP.net core. I have added the connection string to the appsettings.json file below:
{
"ConnectionStrings": {
"MTDatabase": "Server=ss-demo-7-sep112;Database=MTDB;Trusted_Connection=True;"
}
}
but need to find a way to access this connection string in my generic repositry. which is found in a seperate project to my .net core app where it contains all Data Access Layer stuff. Below is my generic repositry where I am trying to get my connection string
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Configuration;
using System.Data.Entity.Core.Objects;
namespace HaldanMT.DAL
{
public class GenericRepository<T> : IDisposable, IRepository<T> where T : class, new()
{
protected ObjectContext _context;
protected ObjectSet<T> _objectSet { get; set; }
public GenericRepository(string connectionName = "MTDatabase")
{
_context = new ObjectContext(ConfigurationManager.ConnectionStrings[connectionName].ConnectionString);
_objectSet = _context.CreateObjectSet<T>();
}
}
}
PS. I have already made reference to this DAL project in my .net core project. The project fails when trying to connect to the DB, the exception is System.NullReferenceException Object reference not set to an instance of an object for the ConnectionString
Any help is greatly appreciated.