8

Question

Is there any ways that I can automatically execute the migration code (EF 7) when publishing my ASP 5 application to IIS using Web Deploy?

I Tried

  • in the project.json, I added this code in the scripts:

    "scripts" : { "prepublish": ["dnx ef database update", "other commands..."], "postpublish": ["dnx ef database update"] }

none worked for me.

Additional Info

I followed the instructions on this link to deploy my ASP 5 RC-1 web application to IIS using web deploy.

After doing so in the publish settings I have:

ASP 5 RC 1 publish to IIS using Web Deploy

Using web deploy in ASP 4 applications I have additional database options:

ASP 4 publish to IIS using Web Deploy

Andrei
  • 36,642
  • 31
  • 139
  • 196
A-Sharabiani
  • 13,270
  • 12
  • 87
  • 109

4 Answers4

6

Use context.Database.Migrate()

You can call this from your Startup class:

using (var context = new MyContext(...))
{
    context.Database.Migrate();
}

It will migrate your database to the latest version on application startup. But be careful doing it, maybe comment out this code and uncommend only when you want to run your migrations.

Andrei
  • 36,642
  • 31
  • 139
  • 196
  • 4
    Be careful doing this. If you have new migrations locally and one of your developers switches their local database connection string to a remote connection string (like test/staging or worse prod) then this will attempt to run the new migrations on the remote database before they are ready. – Troy Grosfield Oct 05 '16 at 21:41
  • @TroyGrosfield makes sense – Andrei Oct 05 '16 at 23:58
  • 5
    Why does @TroyGrosfield have access to the production servers? – THBBFT Oct 28 '16 at 00:01
  • I'm going to create test project for automatic DB deployment and running tests against it. This `context.Database.Migrate()` method helps restore DB every time I run tests. Since I need fresh db created every time I added `context.Database.EnsureDeleted();` a line above to delete DB. – Alezis Dec 28 '16 at 20:13
  • I would be EXTREMELY careful when using this approach, specially at places where security is not that tight and it's VERY common to have, say, the lead developer have access to production databases. – Pepito Fernandez Jun 18 '17 at 17:01
1

Apparently this process does not work now. https://github.com/aspnet/Home/issues/622 After you publish you should find the power shell script with the name of "profile name"-publish.ps1. Then add your commands below these three lines close to the end of this file. You might want to use powershell to make it easier to debug.

'Calling Publish-AspNet' | Write-Verbose

# call Publish-AspNet to perform the publish operation

Publish-AspNet -publishProperties $publishProperties -packOutput $packOutput

Sergey Barskiy
  • 1,713
  • 2
  • 14
  • 16
1

So I added the option -environment to my ef database command. Now it works:

"postpublish": ["dnx ef database update -e Staging"]

I have four different appsettings.json which different connection string for each environment. Just needed to indicate the environment for the command to work.

A-Sharabiani
  • 13,270
  • 12
  • 87
  • 109
0

In you Startup.cs class add this code

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetService<AppDBContext>(); 
            context.Database.Migrate(); 
        }
    }