0

I'm using Entity Framework .Net CORE 2.2 for my database models and in my code I'm using

public void Configure(IApplicationBuilder app, IHostingEnvironment env, MyContextModel MyContext) {
[snipped for clarity]
MyContext.Database.Migrate();
}

to action any migrations into the database. It works fine locally, running any changes as necessary. If I publish my project to Azure, everything builds and uploads fine but when the web site starts I get the message

"An error occurred while starting the application."

in the browser window and the tables remain unchanged. I've removed and republished without MyContext.Database.Migrate(); and the site starts fine, so I'm sure it's something to do with the automatic migration that's causing the problem. I've Googled around and tried putting the line in the DbInitializer and various other places, with no luck.

I've checked Cloud Explorer and I'm logged in so there are no permission issues.

Any ideas what I'm missing or how to whether (and how) I should be reading some Azure log?

Glyn
  • 316
  • 2
  • 20

2 Answers2

0

This approach (MyContext.Database.Migrate();) isn't for everyone. While it's great for apps with a local database, most applications will require more robust deployment strategy like generating SQL scripts.

When you deploy on azure, you could add the following code int the Configure method of Startup.cs

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

For more details, you could refer to this SO thread.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Those lines causes the following error message in the browser when trying to view the site"An error occurred while starting the application. .NET Core 4.6.27317.07 X64 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.2.0-rtm-35687 | Microsoft Windows 10.0.14393|" when I try to run the site. – Glyn Mar 19 '19 at 11:09
  • You could try this [SO thread](https://stackoverflow.com/questions/41915522/asp-net-core-1-1-runs-fine-locally-but-when-publishing-to-azure-says-an-error-o) – Joey Cai Mar 20 '19 at 06:54
0

There's no way to run migrations in Entity Framework CORE apparently. Up until a few months ago, the option to publish in Visual Studio had an "execute migrations" check box, but that's gone now.

You can try to run

dotnet ef database update

or some similar script against the server, but that brought back too many errors to mention. So instead I tried to generate SQL scripts to run along the lines of

dotnet ef migrations script

with various switches and whatnots but that brought back different errors. Eventually I found this.

Script-Migration -from 20190306131332_migration1 -o fileName.sql

which successfully generates SQL for me to run manually against the database.

-from 

is the last migration run, so if I want to get all the changes since migration1 (i.e. migration2, migration3, etc.) migration1 is the -from criteria

This will create a file called filename.sql in the root directory where the .sln file resides.

Glyn
  • 316
  • 2
  • 20