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);
                  }
                );
            });
        }
    }
}
3
  • 1
    Remove this constructor BookContext(DbContextOptions options) and it seems you don't need to override OnConfiguring Commented Oct 11, 2021 at 22:17
  • I did, I still can't add migrations, I get the same error Commented Oct 12, 2021 at 6:03
  • Are you getting the same issue or resolved? Commented Nov 12, 2021 at 23:42

1 Answer 1

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

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

1 Comment

I changed the code to only include one, but i am still getting the error

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.