You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document pitfalls of migrating database when application starts #814
Reacted by Thales Saraiva, Gustavo Rascioni, Piotr Bagrowski, Sam Schneider, Hannah Chan, Shimmy Weitzhandler, Dzmitry Lapunou, GFHuang, Jason Forrestall, Autumn and 5 more
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?
Reacted by GFHuang, Jason Forrestall, gyurisc and Moha Dehghan
@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.
Reacted by Moha Dehghan, Arad Alvand and niektuytel
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.
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:
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
Activity
ajcvickers commentedon Aug 6, 2018
@caztial Can you be more specific about what exactly you want to do at runtime?
Rick-Anderson commentedon Aug 14, 2018
Applying migrations in production
divega commentedon Nov 5, 2018
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.
jwbats commentedon Jan 13, 2019
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?
ajcvickers commentedon Jan 13, 2019
@jwbats The point is that the database migration must be done in a controlled way. The main things to guard against are:
Other considerations are:
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.
MaklaCof commentedon Jan 14, 2019
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.
Rick-Anderson commentedon Jan 14, 2019
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.
MaklaCof commentedon Jan 16, 2019
Agree. This would be done in startup and only by admin. Thinking something like this:
The point is not when migration is performed, but that you do not change the entity class.
14 remaining items
johnnyreilly commentedon Feb 8, 2020
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: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
dazinator commentedon Feb 8, 2020
Also is multiple Migrate() calls happening at once the only concern?
AndriySvyryd commentedon Feb 8, 2020
We are actively working on this: dotnet/efcore#19587
Improvements to migration docs (#2430)