1

How do I get EF to build all the tables for the prod db?

So far I've been building and pushing all my .Net Core database migrations to the dev db

ex.

>dotnet ef migrations add InitialModsN -c Auto2Context

>dotnet ef database update -c Auto2Context

In my Appsettings.Development.json file I have my con string pointing to dev db 'source=DEV'

"ConnectionStrings": {
"DefaultConnection": "Data Source=Auto2.db",
"Auto2": "data source=DEV; initial catalog=Auto2; user id=auto2; password=12345;" }

And my Appsettings.json I have the prod db con string pointing to the prod db 'source=LIVE'

"ConnectionStrings": {
"DefaultConnection": "Data Source=Auto2.db",
"Auto2": "data source=LIVE; initial catalog=Auto2; user id=auto2; password=12345;" }

So far everything is being built and migrated to dev, how do I change this to build all the changes up to date in the prod db?

Do I have to re create an initial migration file and then update? If yes, how do I point it to the prod db? I tried a couple of different commands with no luck. Here is what I tried.

>dotnet ef database update -c Auto2Context --connection "data source=LIVE; initial catalog=Auto2; user id=auto2; password=12345"

-received 'Unrecognized option '--connection''

>dotnet ef database update -c Auto2Context --configuration Release

  • no tables created in the prod db
Community
  • 1
  • 1
chuckd
  • 13,460
  • 29
  • 152
  • 331

2 Answers2

1

Set ASPNETCORE_ENVIRONMENT environment variable before executing the command:

$env:ASPNETCORE_ENVIRONMENT='Production'

Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66
  • Unrecognized option '--environment' and Unrecognized option '-e' – chuckd May 29 '20 at 23:17
  • also I see this: ASPNETCORE_ENVIRONMENT='Production' in a couple of different files. Launchsettings.json and launch.json. – chuckd May 29 '20 at 23:20
  • As far as I know `$env:ASPNETCORE_ENVIRONMENT='Production'`should works [see this](https://stackoverflow.com/a/47452555/1385614) – Mohsen Esmailpour May 30 '20 at 19:29
  • this doesn't seem correct as I would have to go into the file where this line exists and manually change it every time I go in-between dev and prod. That doesn't correct.. – chuckd May 31 '20 at 04:29
0

Here is what I used and it seems to work.

dotnet publish --configuration Release /p:EnvironmentName=Production

but this only builds the application. I had to run this command below to create a sql script to update the prod db.

dotnet ef migrations script -i -o "buildscript.sql" -c Auto2Context

Is this the best/correct way to push db changes to prod?

chuckd
  • 13,460
  • 29
  • 152
  • 331