-
Notifications
You must be signed in to change notification settings - Fork 2k
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 pitfalls of migrating database when application starts #814
Comments
@caztial Can you be more specific about what exactly you want to do at runtime? |
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. |
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? |
@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. |
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. 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. |
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. |
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. |
For this reason, I put my migration update statement in the startup.cs file, before anything accesses it. How else? A database needs to be updated before the new release runs on it. So I do not see a problem here.
Being a full time dev, I never cared much for doing db adminstration. Whenever I have access to the db, I give the db user the db_owner role. Whether my application changes the database or not, depends on what its code does. If I don't provide schema changing code in a part of the application that is not startup.cs (read: the parts that users actually use), then obviously nobody will be able to change the schema by using my application as intended. I don't see much point in db user restrictions. Maybe it's because I never write enterprise stuff. But I see no need for it in my own situation.
I'd use a staging server to test it, first. If it goes well on staging, why would it fail on production? Also, having your application update the schema, does not prevent you from updating the schema manually with ef-generated sql scripts. And what reason do we have to think that ef's generated scripts modifies the schema in a safer manner than ef's update statement in startup.cs?
After deliberation of the above, I'm putting the update statement in startup.cs. I can find no downsides to this for my use case. |
Is there any examples how to migrations in |
+1 More specifically here are some behaviours I was interested to read up on but couldn't find much / any documentation on to do with migrations:
|
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
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 |
Also is multiple Migrate() calls happening at once the only concern? |
We are actively working on this: dotnet/efcore#19587 |
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.
The text was updated successfully, but these errors were encountered: