Set file path for DbContextModelSnapshot file in EF Core - entity-framework-core

I use .NET Web API boilerplate from https://fullstackhero.net/ for an application. As it accesses two databases, I created two DbContext - ApplicationDbContext and FinanceDbContext.
These migration commands are showing a warning
...set different namespaces for migration"
dotnet ef migrations add Initial --project ../Migrators/Migrators.MSSQL/ --context ApplicationDbContext -o Migrations/Application
dotnet ef database update --project ../Migrators/Migrators.MSSQL/ --context ApplicationDbContext
dotnet ef migrations add Initial --project ../Migrators/Migrators.MSSQL/ --context FinanceDbContext -o Migrations/Application
dotnet ef database update --project ../Migrators/Migrators.MSSQL/ --context FinanceDbContext
So I modified the EF Core command with namespace AppDb:
dotnet ef migrations add Initial --namespace AppDb --project ../Migrators/Migrators.MSSQL/ --context ApplicationDbContext -o Migrations/Application
It creates DbContextModelSnapshot file inside the AppDb folder.
Is it possible to create DbContextModelSnapshot file in the Migrations folder?
Thanks.

Related

how to create an initial prisma migration

I have a PostgreSQL database and prisma schema. My migration history is empty and I don't have migrations table. When I am trying to run prisma migrate dev to create migrations table and add initial migration to this table it wants to delete all my database table's data.
prisma migrate dev --name migration_name --create-only command tries to reset my database too.
How to create initial prisma migration without losing my data?
Is it possible to create prisma migrations table in my PostgreSQL database and add initial migration to this table without losing data?
I've tried to follow this article: https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/add-prisma-migrate-to-a-project but it wants to reset my database after db pull.
You can follow these steps to generate the initial migration without resetting your database.
mkdir -p prisma/migrations/init
npx prisma migrate diff --preview-feature --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/init/migration.sql
npx prisma migrate resolve --applied init
npx prisma migrate dev # it should say everything is in sync

update local Dockerized Postgress DB from an equivalent DB on Azure

I have an un-updated local Postgres server, running on a docker container, now I want to update all new records from Production DB,
which runs on Azure Postgres DB.
I'm aware of the pg_dump as in this Answer
but, I'm not clear where should I commend it - the Azure DB doesn't know the Local one and vice versa?
There are multiple methods which you can try.
The most common and simple approach is to use pg_dump and pg_restore commands in bash to upgrade the database.
In above mentioned method, you first create a dump from the source server using pg_dump. Then you restore that dump file to the target server using pg_restore.
To back up an existing PostgreSQL database, run the following command:
pg_dump -Fc -v --host=<host> --username=<name> --dbname=<database name> -f <database>.dump
Once the file has been created, download it in local environment.
After you've created the target database, you can use the pg_restore command and the --dbname parameter to restore the data into the target database from the dump file.
pg_restore -v --no-owner --host=<server name> --port=<port> --username=<user-name> --dbname=<target database name> <database>.dump
To find more upgrade methods, you can refer https://learn.microsoft.com/en-us/azure/postgresql/how-to-upgrade-using-dump-and-restore#method-1-using-pg_dump-and-psql.
To get more details on pg_dump and pg_restore methood, please refer Microsoft Official document Migrate your PostgreSQL database by using dump and restore.

Db2 : Is there any way to copy one database schema tables structure into another database schema..?

I have one db2 database testDb1 with master schema and second one is testDb2 with master schema.
Now my question is how to copy master schema table structure of testDb1 to testDb2 master schema..?
db2look is the answerto your queston as #mao mentioned already.
Run this on the source database
db2look -d testDb1 -e -z master -o db2look_testDb2_master.ddl
You will get a runnable script that could be aesily applied to the target database - just edit the script and change the name of the database in the connect statement.
Run the script with following command (from the command line)
db2 -tvf db2look_testDb2_master.ddl

psql:pr_staging.sql:7624: ERROR: relation "res_company" already exists

Backup command: pg_dump -U username backupdbname -f backupfilename.sql
Restore Command: psql -v ON_ERROR_STOP=1 -f backupfilename.sql -d newdbname;
Actually Tried this command. Backup is working. But while restoring it will throw error psql:pr_staging.sql:7624: ERROR: relation "res_company" already exists. Because for restore, we need one newdb. So that i am creating newdb from browser manually. Thats why i facing the error.
I am creating a new db using terminal command. But it not showing in browser localhost:8069/web/database/selector.
How to restore the backup db?
If you create a db by using Odoo's db manager (interface) there already will be basic tables (the module base will be installed automatically).
There are some ways to restore the db. For example (template0 is a default template db from postgres):
createdb -T template0 newdbname
cat backupfilename | psql newdbname
You shouldn't have a Odoo server running while doing this.
You could also use Odoo's database interface to backup and restore/duplicate databases.

Can dotnet-ef-dbcontext-scaffold ignore schemas?

We're working on a new ASP.Net Core application, and plan on using identity. Currently, we have 2 DbContext files, the ApplicationDbContext file which inherits from IdentityDbContext for the Identity functions, and our own ProjectDbContext for the rest of the tables.
We have all of the tables, both for identity and the rest of our project, in one database. We are using dbcontext-scaffold to create the POCO for our tables. However, it creates POCO files for the identity schema, which we don't need.
You can use two separate contexts for each group of tables using command like below.
IDENTITY DBCONTEXT AND TABLES:
You can change identityDbContextwhat ever you want to specify....
dotnet ef dbcontext scaffold "server=YOUR_SERVER_NAME; Database=YOUR_DB_NAME
rape; Integrated Security = false; Password = YOUR_PASSWORD; User Id = YOUR_USER_ID;" Microsoft.EntityFrameworkCore.SqlServer -o YOUR_OUTPUT_DIR_FOR_DBCONTEXT_AND_ASSOCIATED_TABLES
-c identityDbContext--schema SCHEMA_NAME -t TABLE1 -t TABLE2
PROJECT DBCONTEXT AND TABLES:
You can change projectDbContext what ever you want to specify....
dotnet ef dbcontext scaffold "server=YOUR_SERVER_NAME; Database=YOUR_DB_NAME
rape; Integrated Security = false; Password = YOUR_PASSWORD; User Id = YOUR_USER_ID;" Microsoft.EntityFrameworkCore.SqlServer -o YOUR_OUTPUT_DIR_FOR_DBCONTEXT_AND_ASSOCIATED_TABLES
-c projectDbContext--schema SCHEMA_NAME -t TABLE1 -t TABLE2