How to apply database code first migrations in an

2020-01-30 02:18发布

问题:

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.

I assume you want to update a remote/production database. To do that in a secure manner, you can upload your appsettings.json file with specific credentials and connection string to Secure files in Azure Azure DevOps pipeline. See the picture below.

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 use dotnet ef database update like this :

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 a different name,
  • you don't need to use ls and cat commands - they are for debugging purposes
  • the command dotnet ef database update uses the production appsettings.json that you've uploaded to Secure Files.


回答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.