Skip to content

Document pitfalls of migrating database when application starts #814

Closed

Description

@caztial

Need more code examples to understand how to use the migrations at runtime


Document Details

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

Activity

  1. modified the milestone: Backlog on Aug 6, 2018
  2. ajcvickers commented on Aug 6, 2018

    @ajcvickers
    Contributor

    @caztial Can you be more specific about what exactly you want to do at runtime?

  3. Rick-Anderson commented on Aug 14, 2018

    @Rick-Anderson
    Contributor
  4. added this to the Backlog milestone on Sep 7, 2018
  5. added
    help wanted
    on Nov 5, 2018
  6. divega commented on Nov 5, 2018

    @divega
    Contributor

    I think it is as important to align our guidance (including the guidance provided by the ASP.NET Core documentation) on when and why not to do this.

  7. removed this from the Backlog milestone on Nov 10, 2018
  8. removed
    help wanted
    on Nov 10, 2018
  9. added this to the 2.2.0 milestone on Nov 10, 2018
  10. jwbats commented on Jan 13, 2019

    @jwbats

    I'd love to get some more feedback on why not to use EnsureCreated() and/or Migrate(). What's so robust about generating sql scripts instead? If the ef migrations tool is smart enough to generate 'robust' sql scripts, then why is it not simply using the same robustness in its Migrate() call?

    What if you use a staging server to try out your Migrate() call on and it works well? Why not keep using Migrate() in production until your staging shows you a problem is developing?

  11. ajcvickers commented on Jan 13, 2019

    @ajcvickers
    Contributor

    @jwbats The point is that the database migration must be done in a controlled way. The main things to guard against are:

    • Multiple threads/processes/servers attempting to migrate the database concurrently.
    • Applications trying to access the database while this is happening. (Unless you have very carefully planned migrations that explicitly allow for this.)

    Other considerations are:

    • Often the database permissions to modify the schema should not be given as part of normal application execution.
    • It's sensible to have a clear plan for reverting the migration (and indeed the entire deployment) if it goes wrong.

    It's possible to use Migrate and satisfy all these things. However, just running Migrate when your application starts clearly does not. We would love it if it were easy to run Migrate as part of a controlled application deployment. Realistically, for most people and deployments, a SQL script is easier to integrate into a controlled deployment.

  12. MaklaCof commented on Jan 14, 2019

    @MaklaCof

    Applying migrations at runtime would be awesome feature. Together with System.Linq.Dynamic.Core we could easily implement custom columns.

    For example: let's say you have a table Partners.
    One customer needs additional column PaymentDeadline, so you could add it only to this customer and not to all.
    Another would need SomeOtherColumn.

    It is useless to add all this column to entity. You quickly get to the point where your table Partners have over 200 columns. But most of them are used by 1 or 2 customers.

  13. Rick-Anderson commented on Jan 14, 2019

    @Rick-Anderson
    Contributor

    Allowing customers to add columns to a DB at run time is ultra niche and not something we'd recommend for the majority of apps.

  14. MaklaCof commented on Jan 16, 2019

    @MaklaCof

    Agree. This would be done in startup and only by admin. Thinking something like this:

    • in configuration file admin would add settings for additional fields
    • in startup (before application starts) migrations would be applied based on configuration file

    The point is not when migration is performed, but that you do not change the entity class.

  15. 14 remaining items

  16. modified the milestones: Backlog, 5.0.0 on Jan 15, 2020
  17. johnnyreilly commented on Feb 8, 2020

    @johnnyreilly

    Hey all,

    I've been reading around on best approaches for applying migrations for an ASP.NET Core application in Docker.

    I've been somewhat confused from what I've read. From what @ajcvickers says here: #814 (comment) the suggestion is that applying migrations upon app startup is not considered good practice.

    A particular concern is where you have multiple containers running your app. What if they all attempted migration at once? Given the .Migrate() is not (to my knowledge) idempotent that's unsafe. As @ajcvickers says:

    Multiple threads/processes/servers attempting to migrate the database concurrently.

    But oddly enough, the Microsoft docs seem to advise applying migrations on startup in a docker container. Consider this piece of documentation by the wonderful @julielerman which advocates this approach:

    https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/april/data-points-ef-core-in-a-docker-containerized-app#migrating-the-database

    Despite @ajcvickers advice, there don't seem to be any docs out there showing a good way to apply migrations upon deployment using Docker.

    Are there plans to add some? Or are they out there somewhere and I just haven't stumbled upon them? This question has been looked at by @bricelam before but it doesn't look like that's turned into standard advice that I can see: dotnet/dotnet-docker-samples#89

  18. dazinator commented on Feb 8, 2020

    @dazinator

    Also is multiple Migrate() calls happening at once the only concern?

  19. AndriySvyryd commented on Feb 8, 2020

    @AndriySvyryd
    Member

    We are actively working on this: dotnet/efcore#19587

  20. added a commit that references this issue on Jun 29, 2020
    2c06834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions