【发布时间】: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