How to apply database code first migrations in an azure deployed application?

跟風遠走 提交于 2019-11-26 18:39:32

问题


I just deployed an application into an azure App Service. My project does has migrations to be applied. But I was not able to apply them by using azure app command line

dotnet ef database update

D:\home\site\wwwroot> No executable found matching command "dotnet-ef"

How can I apply them remotely?


回答1:


For dotnet-ef, I assumed that you are talking about Entity Framework Core. I have tried to run dotnet ef database update under D:\home\site\wwwroot of KUDU and Package Manager Console of VS, I could encounter the same issue as you mentioned.

I found a similar issue No executable found matching command "dotnet-ef":

DotNet CLI can only resolve "dotnet-ef" when it is within the project directory.

I could run the commands dotnet ef migrations add, dotnet ef database update via Powershell. For detailed command usage, you could follow EF Core .NET Command-line Tools.

How to apply database code first migrations in an azure deployed application?

Per my understanding, you could not do that. You may need to manually run update-database as vivek nuna answered under Package Manager Console of VS, or you could use Powershell and cd to your project directory, and execute dotnet ef database update to apply any pending migrations for your context to the database, then deploy your application to azure web app.

Additionally, EF does not support Automatic migrations, you may need to manually execute Add-Migration or dotnet ef migrations add for adding migration files. You could explicitly execute the command to apply the migrations, also you could apply migrations in your code. And you could add the following code in the Configure method of Startup.cs file:

using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
    scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
}

Moreover, you could also follow this similar issue.




回答2:


I had a very similiar problem. I am developing a .NET Core 3.0 web app (Visual Studio Angular template with Individual User Accounts). I'm using AzureDevops to build the project and publish it to an App Service on Azure.

First of all, you can use

dotnet tool install --global dotnet-ef --version {version compatible with your project}

in your pipeline to install ef extension for dotnet during the build process.

What I then do is I upload my appsettings.json (with a specific connection string) file to Secure files in my Azure Azure DevOps pipeline.

appsettings.json in Secure files

Then you can reference that file in your build pipeline by Download Secure File task (which downloads that file into the working directory of the build) and then running a tiny cmd script to:

  • move it to your projects directory (see the docs, $(filesName.secureFilePath) retrieves the path to which the file was downloaded),
  • run the migration.

migration script

Note that in the above migration script:

  • app is my projects name,
  • I rename my file to appsettings.json, because I've uploaded it to Secure files with different name,
  • you don't need to use ls and cat commands - they are for debugging purposes.



回答3:


One of the best ways to run migrations on the Azure is running update-database command in the Visual Studio. But this command won't run. Your client IP address should have access to the Azure.

You can follow RUN MIGRATIONS ON THE AZURE. Asp.Net Zero is built on top of Asp.Net Core so these steps will work for you.



来源:https://stackoverflow.com/questions/50243829/how-to-apply-database-code-first-migrations-in-an-azure-deployed-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!