2

Currently i am working on a .Net core 3.1 App. I am using below code in the startup to Add the Dbcontext.

services.AddDbContext<sampleContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

As this is the code first approach i have below code in the Dbcontext

public class sampleContext: DbContext
    {
        public sampleContext()
        {

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

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable("DefaultConnection", EnvironmentVariableTarget.Process));
            }
        }
}

When i am running the API, its working as expected as optionsBuilder.IsConfigured=true.

Appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Initial Catalog=sampleDb; Integrated Security=true;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "UploadFAUrl": "http://localhost:7071/Api"
}

Coming to issue:-

  1. When i am running the CLI command ADD-MIGRATION sampleCongetting **Value cannot be null. (Parameter 'connectionString')**

Whys is so? As we will be moving to different env, we may need to run this command. Atleast in local, we need to run the command. How to fix this issue? Referred some of the question but non helped. PLease suggest if i am missing anything.

9
  • Do you store your connect strings in appsettings.json, can you show us it? Commented Nov 27, 2020 at 6:47
  • Yes its in app settings.json "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Initial Catalog=sampleDb; Integrated Security=true;" } Commented Nov 27, 2020 at 7:01
  • Can you share all your appsettings.json ? Commented Nov 27, 2020 at 7:38
  • Added in the question. Commented Nov 27, 2020 at 7:47
  • As i explained, there is no issue i am getting at runtime. While running Migration commands only facing issue. & there we need to select the project where Dbcontext is threre. Commented Nov 27, 2020 at 8:25

2 Answers 2

1

The EF Core command line tools, will attempt to locate all required services and configuration, based on your current project. By looking for your CreateHostBuilder method, and calling it;

public static IHostBuilder CreateHostBuilder(string[] args) => ...

You shouldn't need to override OnConfiguring. But you may need to provide an explicit --startup-project command line parameter.

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

Comments

1

Do you have multiple projects?

Mark the project Set as startup project which contains Appsettings.json.

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.