skip to Main Content

I used a tutorial from Microsoft to deploy my app to Azure.

When deploying my web app to Azure, the migrations aren’t applying. There is also another error saying that the table "AspNetRoles" already exsits.

It loads the initial page fine, but then when I try clicking on a different page I get this error:
An internal server error showing that migrations haven’t been updated in the database

To be clear, the image is from the deployed web app, I have put it in development mode for now as those errors don’t occur on my local machine.

When I test my app locally, everything runs perfectly and and the database is updated correctly with my new migrations. I expected to receive the same error but I don’t.

I have tried commenting and uncommenting the lines context.Database.EnsureCreated() and context.Database.Migrate() in my Program.cs file and deploying that, but the same error occurs.

I am using the correct connection string.

I have used the SSH and followed these commands:
cd /home/site/wwwroot
./migrate
and got the error message There is already an object named 'AspNetRoles' in the database.

Edit: I have thought the problem might be in my deplyoment on GitHub, as seen here screenshot of github build with the dotnet bundle migration opened. I’m wondering if it’s because I haven’t copied ‘appsettings.json’ alongside my bundle if I need to apply migrations, which is currently the main problem. I’m not sure what it means or how to do exactly that though.

2

Answers


  1. Chosen as BEST ANSWER

    I can't say for certain what the problem was, but I fixed it by adding context.Database.EnsureDeleted() and then context.Database.EnsureCreated() into my Program.cs.

    Here's the block of code, the arrows point to what fixed my problem.

    using (var scope = app.Services.CreateScope())
    {
        var services = scope.ServiceProvider;
        var context = services.GetRequiredService<ApplicationDbContext>();
        context.Database.EnsureDeleted(); // <-- 
        context.Database.EnsureCreated(); // <-- 
    
        // requires using Microsoft.Extensions.Configuration;
        // Set password with the Secret Manager tool.
        // dotnet user-secrets set SeedUserPW <pw>
    
        var testUserPw = "fake_p4ssword_";
    
        await SeedData.Initialize(services, testUserPw);
    }
    

    I no longer get the error that AspNetRoles is already in the database.


  2. Since you can open the page normally after deploying. So we need to check the connectionstrings in your application.

    There are two places we need to check. One is the configuration in the appsettings.json file after publishing.

    enter image description here

    The other is the configuration in azure portal.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search