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 }}
This repository was archived by the owner on Mar 1, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 1, 2018. It is now read-only.
Maybe I overlooked something, but when building a aspnetcore app with EF core, you would need to be able to run:
dotnet ef database update
Now running this during build time, is not possible, especially if you have multiple stages that you would want to deploy this image to.
I guess best solution would be to run the db migration during startup of the container, but how would you go about doing this, since the final image does not contain the sdk only the runtime?
Likely an obvious question, but why does the migration need to run at first run? Is the issue that the database may have changed since the container was built?
@richlander No just an idea, if you have a better suggestion I am open for it :) I just can not do it during build because my build servers do not have access to my production network. Also this seems to be the most common way of doing it. And since the migrations only modify the database if needen it should not hurt anything doing it on container startup (accept for a small delay on app startup, which I could live with).
@richlander Yes I have, but how would that solve my problem? The docker-compose would also be run on my build / dev Server which have no access to the production database, or did I miss something here?
Ok now I can put the pieces together, sorry reading all of the comments directly helps.
So basically during build process I will also run a:
dotnet ef migrations script -o migrate.sql --idempotent
Then I also copy the generated sql script to the runtime image and also create a shell script to run the generated sql script against the production db server. I guess that would work :)
The only problem is that I will also need a commandline client for the used database on the runtime image.
This would be cool to be documented in the README :)
You can call a method in your DbContext to do it:
DbContext.Database.Migrate()
using Microsoft.EntityFrameworkCore
Besides you need to make sure your DB service is up and running before the service tah does this call is bootstrapped. Docker has extensive documentation about this.
I am not sure about the idempotent file solution since you'll usually do some manual data migration sql scripts (migrationBuilder.Sql) which I don't think will be inserted in the generated file.
migrationBuilder.Sql() is treated the same as any other operation--it will be included in the script.
Activity
bricelam commentedon Oct 24, 2017
We should look into automating the same workflow used by the Publish Web dialog:
dotnet ef migrations script --idempotentsqlcmdrichlander commentedon Nov 13, 2017
@bricelam Is there an issue filed for that idea somewhere?
bricelam commentedon Nov 13, 2017
Not that I know of. I'm not sure where it belongs...
richlander commentedon Nov 13, 2017
Likely an obvious question, but why does the migration need to run at first run? Is the issue that the database may have changed since the container was built?
pwfraley commentedon Nov 13, 2017
@richlander No just an idea, if you have a better suggestion I am open for it :) I just can not do it during build because my build servers do not have access to my production network. Also this seems to be the most common way of doing it. And since the migrations only modify the database if needen it should not hurt anything doing it on container startup (accept for a small delay on app startup, which I could live with).
richlander commentedon Nov 13, 2017
Have you looked at docker compose?
pwfraley commentedon Nov 27, 2017
@richlander Yes I have, but how would that solve my problem? The docker-compose would also be run on my build / dev Server which have no access to the production database, or did I miss something here?
pwfraley commentedon Nov 27, 2017
Ok now I can put the pieces together, sorry reading all of the comments directly helps.
So basically during build process I will also run a:
Then I also copy the generated sql script to the runtime image and also create a shell script to run the generated sql script against the production db server. I guess that would work :)
The only problem is that I will also need a commandline client for the used database on the runtime image.
This would be cool to be documented in the README :)
Thank you all for your help.
FlavioRodr commentedon Jan 30, 2018
Actually, you are also able to run migrations programmatically and not only through Package Manager Console (PMC). See https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/migrations
You can call a method in your DbContext to do it:
DbContext.Database.Migrate()
using Microsoft.EntityFrameworkCore
Besides you need to make sure your DB service is up and running before the service tah does this call is bootstrapped. Docker has extensive documentation about this.
bricelam commentedon Jan 30, 2018
migrationBuilder.Sql()is treated the same as any other operation--it will be included in the script.FlavioRodr commentedon Jan 31, 2018
@bricelam you're right.