How to properly handle EF DbContext Migration on Azure SQL DB?

66 Views Asked by At

I've deployed an ASP.NET Core 8 MVC application, written using C#, to the Azure AppServices WebApp by enabling the GitHub Workflow / Actions integration on setting up the Web Application in Azure. Whenever I merge my dev branch into main it builds and deploys to my Azure WebApp.

Now I wanted to use the Azure SQL DB. Connection is setup and access is granted, however, the Identity Provider aspnet tables are not created - the website tells me

There are migrations that have not been applied to the following database(s):

and offers a "Apply Migrations" button which is not working. It only says

An error occurred applying migrations, try applying them from the command line

In the Azure WebApp Log Stream, I do see this error:

The directory or file specified does not exist on the Web server. The URL contains a typographical error. A custom filter or module, such as URLScan, restricts access to the file. Things you can try: Create the content on the Web server. Review the browser URL. Create a tracing rule to track failed requests for this HTTP status code and see which module is calling SetStatus. For more information about creating a tracing rule for failed requests, click here.

Detailed Error

Information:ModuleAspNetCoreModuleV2NotificationExecuteRequestHandlerHandleraspNetCoreError Code0x00000000
Requested URLhttps://mychecker:80/ApplyDatabaseMigrationsPhysical PathC:\home\site\wwwroot\ApplyDatabaseMigrationsLogon MethodAnonymousLogon UserAnonymous

I guess the "Apply Migrations" is only meant for running locally, where it was working fine.

As I am not able to figure out how to run the migration through the Azure Cloud Shell CLI on Azure directly I thought it would be best to integrate the migration part to the GitHub Workflow/Action after the application is built, but I'm not able to figure it out.

I took a look at this answer and added the code to my GitHub workflows .yml file at the end of the deploy steps, but the build just fails with an error

 - name: Update database
        run: |
          dotnet tool install --global dotnet-ef
          dotnet tool restore
          dotnet ef database update -c DataContext -p MyChecker -s ${{env.DOTNET_ROOT}}/myapp

Error output

Run dotnet tool install --global dotnet-ef
Skipping NuGet package signature verification.
You can invoke the tool using the following command: dotnet-ef
Tool 'dotnet-ef' (version '8.0.1') was successfully installed.
Cannot find a manifest file.
For a list of locations searched, specify the "-d" option before the tool name.
No tools were restored.
MSBUILD : error MSB1009: Project file does not exist.
Switch: D:\a\MyChecker\MyChecker\MyChecker
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.
Error: Process completed with exit code 1.

Does anyone have any idea what I am doing wrong?

EDIT: Tried now a few different ways and gave it up as the script is a one off and its not worth to have it generated all the time on each workflow action run.

I generated the migration file through Paket Manager Console by running

Script-Migration
0

There are 0 best solutions below