Azure Function 启动中的 EF Core 迁移 [英] EF Core Migrations in Azure Function startup
问题描述
根据 https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection 在启动完成运行之前,不应使用服务提供程序.确实,如果我尝试获取注册服务,它将失败.
According to https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection the service provider should not be used until AFTER the startup has completed running. Indeed, if I try to get a registered service it will fail.
示例:
[assembly: FunctionsStartup(typeof(Startup))]
namespace Fx {
public sealed class Startup : FunctionsStartup {
public override void Configure(IFunctionsHostBuilder builder) {
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddEnvironmentVariables();
var configuration = configurationBuilder.Build();
builder.Services.AddInfrastructure(configuration);
builder.Services.AddApplication();
var serviceProvider = builder.Services.BuildServiceProvider();
DependencyInjection.AddDatabase(serviceProvider).GetAwaiter().GetResult();
}
}
}
public static class DependencyInjection {
public static async Task AddDatabase(IServiceProvider services) {
using var scope = services.CreateScope();
var serviceProvider = scope.ServiceProvider;
var context = serviceProvider.GetRequiredService<ApplicationDbContext>();
//Error generated here
if (context.Database.IsSqlServer()) {
await context.Database.MigrateAsync();
}
await ApplicationDbContextSeed.SeedSamplePersonnelDataAsync(context);
}
public static IServiceCollection AddInfrastructure(
this IServiceCollection services,
IConfiguration configuration) {
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));
services.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());
return services;
}
}
这会产生以下错误
Microsoft.EntityFrameworkCore: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
是否有在启动期间进行迁移和播种的好选择?
Is there a good option for migrating and seeding during startup?
推荐答案
根据您的代码,我假设您正在构建与 Github 上的 CleanArchitecture 存储库一致的内容.https://github.com/jasontaylordev/CleanArchitecture
According to your code, I assume that you're building something aligned to the CleanArchitecture Repository on Github. https://github.com/jasontaylordev/CleanArchitecture
这个 repo 和你的 apporach 之间的主要区别在于,你显然没有使用 ASP.NET,这根本不是问题,但需要更多的配置工作.
The main difference between this repo and your apporach, is that you're obviously not using ASP.NET, which is not a problem at all, but requires a little bit more configuration work.
文章已经提到(https://markheath.net/post/ef-core-di-azure-functions) 引用了另一篇博文(https://dev.to/azure/using-entity-framework-with-azure-functions-50aa),它简要说明了 EntityFramework Migrations 不能自动发现您的迁移Azure 函数.因此,您需要实现一个IDesignTimeDbContextFactory
的实例.我还在微软文档中偶然发现了它:https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory
The article already mentioned (https://markheath.net/post/ef-core-di-azure-functions) refers another blogpost (https://dev.to/azure/using-entity-framework-with-azure-functions-50aa), which briefly explains that EntityFramework Migrations are not capable of auto-discovering your migrations in an Azure Function. Therefore, you need to implement an instance of IDesignTimeDbContextFactory
. I also stumbled upon it in the microsoft docs:
https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory
例如,您可以将它放在您的 Infrastructure\Persistence\Configurations
文件夹中.(再一次,我只是假设您遵循 CleanArchitecture 存储库结构)
You could for example place it inside your Infrastructure\Persistence\Configurations
folder. (Once again, I'm only assuming that you're following the CleanArchitecture repo structure)
这篇关于Azure Function 启动中的 EF Core 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!