Access connectionString using Iconfiguration in repository

Viewed 78

I'm working with DOTNET6 and i want to use the connection string from Iconfiguration in my repository.

I added it on program.cs :

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var provider = builder.Services.BuildServiceProvider();
var configuration = provider.GetRequiredService<IConfiguration>();

but unable to use it outside in one of the repository files.

Here is the constructor of the repository, but i know it is not the right way to use it:

public class CartsTransactionRepositoryDapper :ICartsTransactionRepositoryDapper
    {
        private IDbConnection db;
        private IConfiguration configuration;
        
        public CartsTransactionRepositoryDapper(IConfiguration configuration) 
        {
            this.db = new SqlConnection(configuration.GetConnectionString("DefaultConnection"));
        }

How can i get the connectionString here?

Thanks

1 Answers

add configuration = configuration in the constructor before connection string declaration.

Related