0

I'm trying to access a connection stored in the application. My solution has two projects, on the one hand, the database project (DAL) and on the other, the WEB API.

On the web api I have an App.settings with the connection string for the two environments.

I'm trying to configure DBContext to take the connection string from there.

What am I doing wrong?

DAL PROJECT:

DBContext:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using DAL.Models.ProcedureModels;
using DAL;
using Microsoft.Extensions.Configuration;

..........

       public DBContext()
        {
        }

        public DBContext(DbContextOptions<DBContext> options)
            : base(options)
        {
        }

.........
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                IConfiguration configuration;
                optionsBuilder.UseSqlServer(configuration.GetConnectionString("local"));
            }
        }

JSON FILE IN WEB API PROJECT:

{
  "ConectionStrings": {
    "local": "............",
    "staging": "..........",
    "staging_ashure": "",
    "production": ""
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

2 Answers 2

1
IConfiguration configuration;
optionsBuilder.UseSqlServer(configuration.GetConnectionString("local"));

This declares a variable configuration which is uninitialized and as such contains no value, and then immediately attempts to use it. This will not work. You would have to initialize the configuration variable first.

But that won’t really help you as you need to actually get the configuration from the web application here.

Instead of using OnConfiguring, I would suggest you to just use the constructor with the DbContextOptions and then rely on the application to properly register the database context from your library.

So inside of the ConfigureServices of your web application’s Startup, add this:

services.AddDbContext<DBContext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("local"));
});

There, DBContext then would be the type from your DAL project.

Sign up to request clarification or add additional context in comments.

Comments

0

If you insist on accessing connectionstring in DBContext, you could try to resolve IConfiguration from Service Collection.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    private readonly IConfiguration _configuration;
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
        IConfiguration configuration)
        : base(options)
    {
        _configuration = configuration;
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            var connectionString = _configuration.GetConnectionString("local");
            optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local"));
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.