【问题标题】:Injecting DBcontext file Azure Function (Http trigger)注入 DBcontext 文件 Azure 函数(Http 触发器)
【发布时间】:2021-11-30 13:09:36
【问题描述】:

我正在尝试使用 Entity frameworkcore 代码 1st 将我的 azure 函数连接到本地数据库,但是当我尝试添加迁移时,我不断收到此错误,

无法解析类型“Microsoft.EntityFrameworkCore.DbContextOptions”的服务 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider 提供程序) 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider 提供程序,类型 instanceType,Object[] 参数) 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider 提供程序,类型类型) 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.c__DisplayClass13_4.b__13()

但我使用与所有应用程序相同的连接字符串,只是不同的数据库

这是我的上下文文件

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");

    }
}

这是我的启动文件

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);
                  }
                );
            });
        }
    }
}

【问题讨论】:

  • 去掉这个构造函数BookContext(DbContextOptions options),看来你不需要重写OnConfiguring
  • 我做了,我仍然无法添加迁移,我得到同样的错误
  • 您是否遇到同样的问题或已解决?

标签: asp.net-core entity-framework-core azure-functions azure-http-trigger


【解决方案1】:

DbContextPool 需要单个公共构造函数。

您需要使用AddDbContextAddDbContextPool,而不是两者都使用。 检查下面的示例并尝试:

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

更多信息请参考MSFT documentation

【讨论】:

  • 我将代码更改为只包含一个,但我仍然收到错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 2021-01-25
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多