0

This is in my startup file (Dependency Injection)

public override void Configure(IFunctionsHostBuilder builder)
    {
       

        builder.Services.AddDbContext<CatsDBContext>(
            options =>
            {
                const string Name = "SqlConnectionString";
                options.UseSqlServer(Configuration.GetConnectionString(Name));
            });

This is my local.settings.json

{
    "IsEncrypted": false,   "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"   },
     "ConnectionStrings": {
    "SqlConnectionString": "Server=tcp:animal77.database.windows.net,1433;Initial Catalog=CatsDB;Persist Security Info=False;User ID=j;Password=A;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;"
       } }

This is my context

namespace Shizzle.Models
{
    public partial class CatsDBContext : DbContext
    {
        public CatsDBContext()
        {
        }

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

        public virtual DbSet<Cats> Cats { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning 
          //  optionsBuilder.UseSqlServer("Server=tcp:animal77.database.windows.net,1433;Initial Catalog=CatsDB;Persist Security Info=False;User ID=j;Password='';MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
            }

This is my function that I want it to do to the db

namespace Shizzle
{
    public  class Function1
    {
        private readonly CatsDBContext _applicationDbContext;

        public Function1(CatsDBContext applicationDbContext)
        {
            _applicationDbContext = applicationDbContext;
        }

        [FunctionName("Function1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            
            string name = req.Query["name"];

            var demo = new Cats { Name = "Jason", Id = 5 };
            _applicationDbContext.Cats.Add(demo);
            _applicationDbContext.SaveChanges();

and this is my startup DI class :

[assembly: FunctionsStartup(typeof(Startup))]

namespace Shizzle
{
    class Startup : FunctionsStartup
     {

       public IConfiguration Configuration { get;  }

        public override void Configure(IFunctionsHostBuilder builder)
        {
           

            builder.Services.AddDbContext<CatsDBContext>(
                options =>
                {
                    const string Name = "SqlConnectionString";
                    options.UseSqlServer(Configuration.GetConnectionString(Name));
                });
            

        }

        }
   
}

And I am getting this error in Visual Studio 2019

enter image description here

What can I try next?

6
  • You are missing an 's'. Commented Aug 26, 2021 at 11:41
  • For one thing, the connection strings object should be within "values" object. Not sure if the object works in there though, you could also instead use a top-level setting within the values object. Commented Aug 26, 2021 at 11:41
  • where am I missing the s ? Commented Aug 26, 2021 at 11:42
  • if I add it in their the intelisense does not bring up ConnectionStrings, whereas where I put it, it does. Commented Aug 26, 2021 at 11:44
  • Where is the logic where you build your configuration? Where does your Configuration object come from? Commented Aug 26, 2021 at 11:58

1 Answer 1

1

You're missing the logic to build your configuration

the best way in my opinion is to also override ConfigureAppConfiguration

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder) 
{
  Configuration = builder.ConfigurationBuilder.Build();
}

But you might also be able to add this in your Configure function

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

1 Comment

Top Man ! Thanks you

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.