[英]EFCore migration issue with Azure Function App built using Clean Architecture
I have created an Azure Function App
using .Net Core
with Clean Architecture as defined here :我创建了一个Azure Function App
,使用.Net Core
和 Clean Architecture,定义如下:
This is how my Project Structure
looks like:这就是我的Project Structure
的样子:
The Entity Framework is implemented in the Infrastructure Layer
and it looks like this:实体框架在Infrastructure Layer
中实现,如下所示:
ApplicationDbContext Code & DI inside Infrastructure基础设施中的 ApplicationDbContext 代码和 DI
namespace AppFunctions.Infrastructure.Persistence
{
public class ApplicationDbContext : DbContext, IApplicationDbContext
{
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
public Task<int> SaveChangesAsync()
{
return base.SaveChangesAsync();
}
}
}
namespace AppFunctions.Infrastructure
{
public static class DependencyInjection
{
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)),ServiceLifetime.Transient);
services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
return services;
}
}
}
And this DI is registered in Azure Function App's Startup class
like this:而这个 DI 注册在Azure Function App's Startup class
是这样的:
[assembly: FunctionsStartup(typeof(StartUp))]
namespace JSStockValuationFrameworkAppFunctions
{
internal class StartUp : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
ConfigureServices(builder.Services);
}
private void ConfigureServices(IServiceCollection services)
{
// Configurations
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
services.AddApplication();
services.AddInfrastructure(configuration);
}
}
}
Here, I'm facing an issue with Migration.在这里,我面临着迁移问题。 I tried the following command:我尝试了以下命令:
dotnet ef migrations add "SampleMigration" --project Infrastructure --startup-project FunctionApp --output-dir Persistence\Migrations
But getting this error:但是得到这个错误:
MSBUILD : error MSB1009: Project file does not exist.
Switch: C:\FrameworkAppFunctions\AppFunctions
Unable to retrieve project metadata. Ensure it's an SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.
SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option. ```
do.net ef dbcontext scaffold <list_of_options>
command from the parent folder which consists of Solution file in it.它可以通过从包含解决方案文件的父文件夹运行do.net ef dbcontext scaffold <list_of_options>
命令来解决。 Also, use cd..
and rerun the command which will give you the result.此外,使用cd..
并重新运行将为您提供结果的命令。\
in you command ( Persistence\Migrations
) change all with forward slash /
此外,我可以看到您在命令( Persistence\Migrations
)中使用反斜杠\
将所有内容更改为正斜杠/
The problem has been resolved with the following code IDesignTimeDbContextFactory
该问题已通过以下代码解决IDesignTimeDbContextFactory
namespace Infrastructure.Persistence.Configuration
{
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("Connection string goes here...");
return new ApplicationDbContext(optionsBuilder.Options);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.