简体   繁体   English

EFCore 迁移问题 Azure Function 使用 Clean Architecture 构建的应用程序

[英]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. ```
  • It could be resolved by running 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..并重新运行将为您提供结果的命令。
  • Also, I can see you are using back slashes \ in you command ( Persistence\Migrations ) change all with forward slash /此外,我可以看到您在命令( Persistence\Migrations )中使用反斜杠\将所有内容更改为正斜杠/
  • For more information you can go through this link .欲了解更多信息,您可以通过此链接拨打 go。

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.

相关问题 如何使用Serilog记录器有效禁用Azure Function(隔离模式)中的内置日志? - How to effectively disable built-in logs in Azure Function (isolated mode) using Serilog logger? 使用现有的 .NET 和 Azure Function APP - Using existing VNET with Azure Function APP Azure 逻辑应用程序 - 内置连接器与托管连接器 - Azure Logic App - Built In vs Managed connectors Azure function 使用应用程序设置无法访问运行时 - Azure function runtime unreachable using app settings EF 迁移错误 - Azure - App 上的 MySQL - Error on EF migration - Azure - MySQL on App 在 Azure 中的 function 应用程序中使用 azcopy login --identity - Using azcopy login --identity from within a function app in Azure 部署Azure function到微软Azure中的应用服务 - Deploy Azure function to app service in Microsoft Azure Azure 自动化 - 无法停止 Azure Function App - Azure Automation - Cannot stop Azure Function App 使用来自 Azure 的托管身份连接 Azure CosmosDB 在本地和 Azure 上的应用程序 Function - Connect Azure CosmosDB using Managed Identities from Azure Function App both locally and on Azure 在 Azure Data Studio 上使用 Azure SQL 迁移扩展 - Using Azure SQL Migration extension on Azure Data Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM