0

I am trying to connect my azure function to a local DB using Entity frameworkcore code 1st but I keep on getting this error when I try to add migrations,

Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.b__13()

but I am using the same connection string i use for all my app, just a different DB

This is my context file

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace FunctionApp36
{
    class BookContext :DbContext
    {

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

        public BookContext(DbContextOptions options) : base(options)
        {
        }

        public BookContext() : base()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlServer("Data Source=ABS\\SQLEXPRESS;Initial Catalog=Ba;Integrated Security=True");

    }
}

and this is my startup file

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using FunctionApp36;

[assembly: WebJobsStartup(typeof(StartUp))]
namespace FunctionApp36
{
    public class StartUp : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            var config = new ConfigurationBuilder()
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            builder.Services.AddDbContext<BookContext>(options1 =>
            {
                options1.UseSqlServer(
                  config["ConnectionStrings:DefaultConnection"],
                  builder =>
                  {
                      builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
                      builder.CommandTimeout(10);
                  }
                );
            });
        }
    }
}
johnson dubula
  • 31
  • 1
  • 1
  • 7

1 Answers1

0

DbContextPool needs single public constructor.

You either need to use AddDbContext or AddDbContextPool, not both of them. Check my Example below and try:

public partial class MyDbContext : DbContext
{
    private readonly ... //Code
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
        //Code
    }
}

For more information, please refer this MSFT documentation