Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document where to place code for applying migrations at runtime #2369

Closed
Eli-Black-Work opened this issue May 8, 2020 — with docs.microsoft.com · 1 comment · Fixed by #2430
Closed

Document where to place code for applying migrations at runtime #2369

Eli-Black-Work opened this issue May 8, 2020 — with docs.microsoft.com · 1 comment · Fixed by #2430

Comments

Copy link

The "Apply migrations at runtime" section contains code but doesn't say where in your app to put it. I found the answer at https://stackoverflow.com/a/38263675, but it would be nice if the article mentioned where this code should be placed within your app :)


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@bricelam
Copy link
Contributor

bricelam commented May 8, 2020

If you understand the security risks of using a connection string with permissions to alter the database schema and you understand problems that can occur when multiple instances of your app try to migrate the database at the same time...

You can do it in Program.Main. If you're using Microsoft.Extensions.Hosting, it would look like this:

public static void Main(string[] args)
{
    var host = CreateHostBuilder(args).Build();

    using (var scope = host.Services.CreateScope())
    {
        var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
        db.Database.Migrate();
    }

    host.Run();
}

You could also consider adding an admin page that requires the user to be authenticated before clicking a button to migrate the database.

@ajcvickers ajcvickers added this to the Backlog milestone May 9, 2020
roji added a commit that referenced this issue Jun 29, 2020
Closes #814
Closes #694
Closes #2369
Closes #2188
Closes #1048
Part of #691
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants