1
votes

I am attempting to use entity framework against a database in Azure. I have a connection string stored in the local.settings.json, but all of my attempts to reference it from a Startup.cs have failed.

I am able to access my connection string using an IConfiguration DI into a function class and I can successfully access the database with SQL Command using the configuration like:

string connectionString = _configuration.GetConnectionString("cpni");

So I know that the connection string is working and that I can access it.

If I try to use DI with IConfiguration on the Startup class, the compiler does not give me any errors, but once it's running in debug I begin getting the following error:

System.Private.CoreLib: No parameterless constructor defined for type 'CPNI_Functions.Startup'.

Here is what I'm currently successfully using with a hardcoded connectionString (since using DI with IConfiguration isn't working):

builder.Services.AddDbContext<CpniContext>(
    options => options.UseSqlServer(connectionString));

And that allows me to work with the database through EF. What I would like to use is some combination of that and something like this:

builder.Services.AddDbContext<CpniContext>(/*options need to go here?*/)
    .Configure<IConfiguration>((configuration) =>
    {
        //Or are options supposed to go here somewhere, or be bound here with some sort of .Bind()?
        configuration.GetConnectionString("cpni");
    });

If that doesn't make sense or won't work, then please let me know what the recommended way of setting this DI up is. If possible, I want to avoid using configuration lookup through ConfigurationManager.

For reference, here is the full Startup.cs and this works:

using CPNI_Functions.Data;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(CPNI_Functions.Startup))]

namespace CPNI_Functions
{
    class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            string connectionString = "myconnectionstringhere";

            builder.Services.AddDbContext<CpniContext>(
                options => options.UseSqlServer(connectionString));
        }
        /*
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<CpniContext>(configuration.GetConnectionString("cpni"));
        }*/
    }
}

I'm new to core and I'm new to Azure Functions, so please forgive my ignorance on this. Thank you in advance for your help.

1

1 Answers

1
votes

For an Azure function app you could try adding NuGet package:

System.Configuration.ConfigurationManager

local.settings.json:

{
  "ConnectionStrings": {
    "cpni": "[ConnectionStringDetails]"
  }
}

Then hopefully you should be able to access using ConfigurationManager.

services.AddDbContext<CpniContext>(
    options => options.UseSqlServer(ConfigurationManager.ConnectionStrings["cpni"].ConnectionString));