使用干净体系结构构建的Azure函数应用程序的EFCORE迁移问题

发布于 2025-01-23 07:01:07 字数 3267 浏览 5 评论 0原文

我已经使用.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:

enter image description here

The Entity Framework is implemented in the Infrastructure Layer and it looks like this:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2025-01-30 07:01:07
  SDK风格的项目。如果您使用的是自定义baseinteriateOutputpath
或msbuildProjectExtensionsPath值,使用
-MSBuildProjectExtensionsPath选项。 ````````
 
  • 可以通过从父级文件夹中运行dotnet ef dbcontext脚手架&lt; list_of_options&gt;命令来解决它。另外,使用cd ..并重新运行命令,这将为您带来结果。
  • 另外,我可以看到您在命令中使用Back Slashes \persistence \ opripations)使用forward slash /更改所有信息,
  • 以获取更多信息。可以通过此 link
SDK-style project. If you're using a custom BaseIntermediateOutputPath
or MSBuildProjectExtensionsPath values, Use the
--msbuildprojectextensionspath option. ```
  • It could be resolved by running dotnet ef dbcontext scaffold <list_of_options> command from the parent folder which consists of Solution file in it. Also, use cd .. and rerun the command which will give you the result.
  • Also, I can see you are using back slashes \ in you command (Persistence\Migrations) change all with forward slash /
  • For more information you can go through this link.
小红帽 2025-01-30 07:01:07

该问题已通过以下代码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);
        }
    }
}

The problem has been resolved with the following code 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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文