简体   繁体   English

有关如何在多个Azure SQL数据库,连接字符串和Azure App Services发布配置文件中使用迁移的完整指南

[英]Complete guide on how to use Migrations with several Azure SQL databases, connectionstrings and Azure App Services publish profiles

I have a MVC project hosted in Azure App Services with Azure SQL databases for the data. 我在Azure应用服务中托管了一个MVC项目,其中包含用于数据的Azure SQL数据库。 I have a development, staging/acceptance and production environment (App Service) and as for the databases i have development, staging/acceptance, production and a local DB (mdf). 我有一个开发,暂存/接受和生产环境(应用程序服务),对于数据库,我也有一个开发,暂存/接受,生产和本地数据库(mdf)。 I have 3 publish profiles (1 for each environment). 我有3个发布配置文件(每个环境1个)。

I (think) to understand that i can use the CTOR of the DbContext class to set a connectionstring from the web.config based on the name: 我(认为)理解我可以使用DbContext类的CTOR基于名称从web.config设置连接字符串:

Ex. 防爆。

public ApplicationDbContext(): base("DbContextNameThatResidesInWebConfig") 
{           
     // ...               
}   

If i am not using migrations i am able to do what i want without any issues. 如果我不使用迁移程序,那么我可以做我想做的事而不会出现任何问题。 When using migrations (since i don't want any data loss when changing my model) i am getting several issues on model creation, i tried using several methods in my above CTOR: 使用迁移时(由于更改模型时我不希望丢失任何数据),在创建模型时遇到了一些问题,我在上面的CTOR中尝试了几种方法:

// this is a custom Seeder that inherits DropDatabaseAlways/WhenModelChanges
System.Data.Entity.Database.SetInitializer(new ContractCareSeeder()); 

// from what i understand, this tells to use the latest migrations applied
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Migrations.Configuration>());

Configuration.cs: Configuration.cs:

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    // seeding method ...
}    

But i always receive errors like, "there is already a table called XXX ..." even when i have succesfully executed the Update-Database command in the console it still fails. 但是,即使我在控制台中成功执行了Update-Database命令,我仍然会收到诸如“已经有一个名为XXX ...的表”之类的错误,但它仍然会失败。

I am quite lost in this and can't seem to find what is the best way of handling my scenario. 我对此颇为迷惑,似乎找不到解决我的情况的最佳方法。 I want to be able to use multiple publish profiles (later on we will use development slots but not now) and there there is also configuration possible. 我希望能够使用多个发布配置文件(稍后我们将使用开发插槽,但现在不使用),并且还可以进行配置。 I can see all my connections from my web.config along with update database checkbox but i can't seem to find the right way of configuring everything together... 我可以看到来自我的web.config的所有连接以及“更新数据库”复选框,但是我似乎找不到正确的方式来配置所有内容……

Can anyone assist me, suggest me or provide any help of any kind? 谁能帮助我,建议我或提供任何帮助? Kind regards! 亲切的问候!

ASP.NET 5 introduces improved support for controlling application behavior across multiple environments , such as development , staging , and production . ASP.NET 5引入了改进的支持,可以跨多个环境控制应用程序行为,例如developmentstagingproduction Environment variables are used to indicate which environment the application is running in, allowing the app to be configured appropriately. 环境变量用于指示应用程序在哪个环境中运行,从而可以正确配置应用程序。

You could set ASPNET_ENV with the current environment you want to use. 您可以将ASPNET_ENV设置为ASPNET_ENV使用的当前环境。 If you need to check whether the application is running in a particular environment, use env.IsEnvironment("environmentname") 如果需要检查应用程序是否在特定环境中运行,请使用env.IsEnvironment("environmentname")

if (env.IsDevelopment())
{
    app.UseBrowserLink();
    app.UseDeveloperExceptionPage();
    app.UseDatabaseErrorPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
}

For more details you could refer to this article and this one which though is asp.net core. 有关详细信息,你可以参考这个文章和这一个这虽然是asp.net的核心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM