1

I have below Folder structure

All below are in a single solution:-

  1. API project (All API related Controller,Helper, Extension Methods)
  2. BLL Class library Project(for all business Layer logic)
  3. DAL Class Library Project(Here the EF Core Db context class is there).
  4. Model Class library(where all Model that are used in the API, EF core are added)
  5. Function App Project( Where there a call to Database to do some checks)

Issue:- There is a API from (1), which gives a call to the Function App(5). Hence in order to the Db check & validation I am initiating the Dbcontext().

    using (var _dbcontext = new sampleDbcontext())
       {
         //Code to validate some Db check 
       }

sampleDbcontext is there in the DAL Class Library (3) where EFcore Dbcontext class is there. so default there is a project reference added to the function app project from DAL Project. In the Dbcontext class i have the below code

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

This DefaultConnection is there in the API project (1). i.e. in the appsettings.json file.

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Initial Catalog=sampleDb; Integrated Security=true;"
  }
}

Every time the Function is executing, its throwing error Value cannot be null. (Parameter 'connectionString'). I am really clueless where i am missing what.

I have added the reference of the APi project(1) from the function App(5), still no result. Reference tree as below

  1. Api project --BLL(2) & model class library(4)
  2. BLL Class library --DAL(3) & model class library(4)
  3. DAL Class library -- model class library(4)
  4. Function App Project --BLL Class Library (2)

Starting project is API project (1). Where is the possible issue?

5
  • Maybe I'm wrong, but settings within the appsettings.json are not avaiable as environment variable. Also in you appsettings.json you have the structure ConnectionStrings - DefaultConnection, but in your code you try to get DefaultConnection. So this seems to be the error. Commented Jan 15, 2021 at 8:32
  • Maybe take a look at stackoverflow.com/a/31453663/1838048 Commented Jan 15, 2021 at 8:33
  • you mean optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable("ConnectionStrings:DefaultConnection", EnvironmentVariableTarget.Process)); Commented Jan 15, 2021 at 8:34
  • But this is working as expected for rest all API, where there is no Function App call. This is only failing for the API which have a call to Function app. In Dbcontext i am not using IConfiguration , as it will tend me for a extra DI. so i am using Environment.GetEnvironmentVariable("DefaultConnection", EnvironmentVariableTarget.Process) Commented Jan 15, 2021 at 8:35
  • Yes, if you put the result of Environment.GetEnvironmentVariable(...) into its own variable, before you call options.UseSqlServer() and set a breakpoint there you can check if it contains the desired value. Commented Jan 15, 2021 at 8:36

1 Answer 1

1

I was missing with the Initiation of the DbContext in the Function App StartUp. While i was creating the SampleDbcontext as below

using (var _dbcontext = new sampleDbcontext())
       {
         //Code to validate some Db check 
       }

sampleDbcontext() was working ok as the Namespace was added but no object instance was getting assigned to it.

Every time the Empty constructor was getting a call, where there was no Db connection string was mentioned. Connection was basically in the override void OnConfiguring(DbContextOptionsBuilder optionsBuilder). It needed the StartUp Service Layer injection from the Function App & as usual DI fixed the issue.

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

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.