Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何在Azure应用程序服务中使用代码优先迁移_Entity Framework_Azure_Visual Studio 2017_Azure Web App Service - Fatal编程技术网

Entity framework 如何在Azure应用程序服务中使用代码优先迁移

Entity framework 如何在Azure应用程序服务中使用代码优先迁移,entity-framework,azure,visual-studio-2017,azure-web-app-service,Entity Framework,Azure,Visual Studio 2017,Azure Web App Service,当我在本地运行时,我会手动运行下面的命令,然后将应用打包并发布到我的IIS服务器上 Add-Migration Initial Update-Database 当我想发布到azure appservice时,这些命令会自动运行吗?如果是这样,当我将其发布到azure时,它如何知道使用不同的ConnectionString 我在appsettings.json中添加了azure的connectionString,但我不明白在发布到azure AppServices时如何告诉我的控制器等使用它 "

当我在本地运行时,我会手动运行下面的命令,然后将应用打包并发布到我的IIS服务器上

Add-Migration Initial
Update-Database
当我想发布到azure appservice时,这些命令会自动运行吗?如果是这样,当我将其发布到azure时,它如何知道使用不同的ConnectionString

我在appsettings.json中添加了azure的connectionString,但我不明白在发布到azure AppServices时如何告诉我的控制器等使用它

"ConnectionStrings": {
    "AzureTestConnection": "Data Source=tcp:xxxxxx-test.database.windows.net,1433;Initial Catalog=xxxxx;User Id=xxx@yyyy.database.windows.net;Password=xxxxxx",
    "NWMposBackendContext": "Server=(localdb)\\mssqllocaldb;Database=NWMposBackendContext-573f6261-6657-4916-b5dc-1ebd06f7401b;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
我正在尝试使用不同的连接字符串创建三个配置文件

  • 本地的
  • 发布到AzureApp测试
  • 发布到AzureApp产品
  • 当我想发布到azure appservice时,这些命令会自动运行吗

    EF不支持自动迁移,您可能需要手动执行添加迁移或dotnet EF migrations Add以添加迁移文件。您可以显式执行命令来应用迁移,也可以

    您可以在Startup.cs文件的Configure方法中添加以下代码:

    using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
    {
        scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
    }
    
    注意:您还可以将门户上数据库中的connectionstring设置为此处,然后可以在本地对其进行测试,并可以使用debug进行故障排除

    另外,您可以尝试使用一个connectionstring进行测试,以确保连接到数据库时没有问题

    4.使用
    app.UseDeveloperExceptionPage()启用开发者异常页面
    和启动类中的
    app.UseExceptionHandler
    方法,这些方法将显示错误

            public Startup(IHostingEnvironment env)
            {
                Configuration = new ConfigurationBuilder()
                    .SetBasePath(env.ContentRootPath)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .Build();
    
                HostingEnvironment = env;
            }
    
            public IConfigurationRoot Configuration { get; }
            public IHostingEnvironment HostingEnvironment { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                if (HostingEnvironment.IsDevelopment())
                {
                    services.AddDbContext<SchoolContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
                }
                else
                {
                    services.AddDbContext<SchoolContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("azure")));
                }
                services.AddMvc();
            }
    
    公共启动(IHostingEnvironment环境)
    {
    配置=新的ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
    .Build();
    HostingEnvironment=env;
    }
    公共IConfigurationRoot配置{get;}
    公共IHostingEnvironment主机环境{get;}
    //此方法由运行时调用。使用此方法向容器中添加服务。
    public void配置服务(IServiceCollection服务)
    {
    if(HostingEnvironment.IsDevelopment())
    {
    services.AddDbContext(选项=>
    options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
    }
    其他的
    {
    services.AddDbContext(选项=>
    options.UseSqlServer(Configuration.GetConnectionString(“azure”));
    }
    services.AddMvc();
    }
    

    有关更多详细信息,您可以参考此项。

    您是否确保您的网站是asp.net而不是asp.net核心?其asp.net核心确实是我想要使用的,这有关系吗?没关系,我只是想确认一下,因为web应用程序没有appsetting.json。我在哪里可以找到此选项?在webapp>property>debug中将ASPNETCORE_环境值设置为azure。正如我所说,您可以在本地设置它,右键单击您的应用程序,然后单击property>debug。当您发布到azure时,该设置也适用。是的,但我不仅有调试配置文件,我还需要多个配置文件,不仅仅是开发和生产,我不知道如何在您的解决方案中添加更多环境,如测试、开发、QA、Prod。我知道您有多个配置文件。ASPNETCORE_环境值表示要使用的当前配置文件。您所有具有不同设置的配置文件都位于ConfigureServices中。当您的ASPNETCORE_环境值为test时,它将做出判断并转到test connectionstring。我理解这一点,但我不理解在何处添加环境
            public Startup(IHostingEnvironment env)
            {
                Configuration = new ConfigurationBuilder()
                    .SetBasePath(env.ContentRootPath)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .Build();
    
                HostingEnvironment = env;
            }
    
            public IConfigurationRoot Configuration { get; }
            public IHostingEnvironment HostingEnvironment { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                if (HostingEnvironment.IsDevelopment())
                {
                    services.AddDbContext<SchoolContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
                }
                else
                {
                    services.AddDbContext<SchoolContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("azure")));
                }
                services.AddMvc();
            }