使用干净体系结构构建的Azure函数应用程序的EFCORE迁移问题
我已经使用.net core
使用azure函数应用程序
用干净的体系结构为定义 there :
这就是我的项目结构
看起来像:
实体框架是在基础结构层中实现的
看起来像这样:
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;
}
}
}
和此DI在azure function应用程序的启动类
中注册,例如:
[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);
}
}
}
在这里,我面临迁移的问题。我尝试了以下命令:
dotnet ef migrations add "SampleMigration" --project Infrastructure --startup-project FunctionApp --output-dir Persistence\Migrations
但是获取此错误:
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.
I have created an Azure Function App
using .Net Core
with Clean Architecture as defined here:
This is how my Project Structure
looks like:
The Entity Framework is implemented in the Infrastructure Layer
and it looks like this:
ApplicationDbContext Code & DI inside Infrastructure
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:
[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(quot;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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dotnet ef dbcontext脚手架&lt; list_of_options&gt;
命令来解决它。另外,使用cd ..
并重新运行命令,这将为您带来结果。\
(persistence \ opripations
)使用forward slash/
更改所有信息,dotnet ef dbcontext scaffold <list_of_options>
command from the parent folder which consists of Solution file in it. Also, usecd ..
and rerun the command which will give you the result.\
in you command (Persistence\Migrations
) change all with forward slash/
该问题已通过以下代码
idesigntimedBcontextFactory
解决了问题。The problem has been resolved with the following code
IDesignTimeDbContextFactory