During development, I often want to clear out the data in my Entity Framework (SQL) database and rebuild the empty table structure using existing migrations. I'd like to do this with dotnet ef commands.
Dropping the DB is easy:
dotnet ef database drop {OPTIONAL: --project MyProject --context MyContext}
I had thought I could rescaffold with the update command:
dotnet ef database update {OPTIONAL: --project MyProject --context MyContext}
But this fails with a "login failed for user" error. My DB is hosted in Azure, so I'm having to manually recreate the database in the portal, update my connection string and then run the update command.
Related
I have a solution consisting of 4 projects:
EntityProject : contains POCO classes.
ContextProject : contains database context derived from DbContext, has a reference to EntityProject.
MigrationProject : contains public class Empty{}, has a reference to ContextProject.
StartupProject : for example Asp.Net Core Webapi, has references to ContextProject and MigrationProject.
In StartupProject, I invoke MigrationAssembly(typeof(MigrationProject.Empty).Assembly.GetName().Name) to change the migration assembly project from the default ContextProject to MigrationProject.
If the current working directory is the solution directory, I usually do the following and it works.
migration with
dotnet ef migrations add Initial --project MigrationProject --startup-project StartupProject
where --project is mandatory.
updating database with
dotnet ef database update --startup-project StartupProject
where --project is omitted.
Now I am wondering why dotnet ef database update also has --project switch, what is it for? Is there an example in which --project is mandatory?
Any comments are always welcome!
It is just a general parameter not used by the Update database code path
See source here: https://github.com/dotnet/efcore/blob/main/src/EFCore.Design/Design/Internal/MigrationsOperations.cs#L197
I'm new to Prisma ORM, & I'm trying to make migrations in Prisma
I see that I way to do this is to update data.model & then just run:
prisma deploy
But what if I want to create a migrations for specific versions of app how could I do that ??
As the prisma documentation describes there are two ways of doing database migrations in prisma:
Using the Prisma CLI
Performing a manual DB migration with plain SQL
If you follow the first approach and edit your data model the changes will be carried out automagically once you run prisma deploy. You can specify the service and stage this will be rolled out to via the PRISMA_ENDPOINT environment variable:
PRISMA_ENDPOINT="http://localhost:4466/{SERVICE}/{STAGE}"
This way you can roll out and test you data model changes in a different stage or on a different service.
The second approach is to manually change the database model via plain SQL. Be careful to ensure the database schema and your data model are in sync.
For more information check out:
https://www.prisma.io/docs/datamodel-and-migrations/migrations-POSTGRES-asd4/
I successfully created an Entity Framework Core migration and updated the database with it.
Then after I added another class, I created a second migration called "update1" which created a class of the same name from the command line tools.
However, when I attempt to update the database, it fails.
Here is the commands I used
dotnet ef migrations add update1 -c MyDbContext
dotnet ef database update update1 -c MyDbContext
and it failed with
There is already an object named MyTable in the database
which is a table which was created in the initial migration.
How can I tell it to either ignore the error, or else to only attempt to run the update1 migration?
Edit: deleting the table that was already there caused this odd behavior to stop happening and now it works as expected.
Thanks
You have to remove the unwanted migration from the __MigrationHistory table.After that you can run your latest migration.This is happened due to you have manually deleted the table.B'cos EF doesn't know anything about your manual operations hence __MigrationHistory table still exist your old migration details (i.e. manually deleted table's record).
In my case I used Ubuntu, EF 6 and the database was in the docker and it's doesn't updated DB.
I added --connection attribute and it's works, an example:
sudo dotnet ef database update --connection
"Server=localhost;Database=temp;User Id=sa;Password=xxxxxxx;"
I am working on a web project using ASP.Net 5 and EF7.
I have imported all the tables from existing database onto my models in my project. However, I am having problems in regards with migrations.
I have created the initial migration, made some changes on particular entity, create another migration following the changes I have made and now want to apply the changes on the database.
After running this command below:
dnx ef database update [Migration]
the dnx is trying to apply the 'initial' migration with all the entities which are already in database and this causes an error as below:
{ There is already an object named ['EntityName'] in the database. }
Can you please advise on how to do the migration on the existing database?
Thanks
Saeed
In EF6 you would run a migration with the -IgnoreChanges flag and it would take a snapshot of the model without any Up() code. This is missing from EF 7(EF Core) as indicated here.
The workaround for now is delete or comment out the code for existing database objects from the Up() code of the migration and then update-database. Subsequent migrations will then include only the changes.
After 2 days I found a way for EFCore that is not in google and internet!
How My steps works?
When you have a database with 10 table and you have data in tabales that you don't want to clear's data.and after this you will create new models in your code first and runnig to existing database and you will get error "Invalid object name 'tableName'." for query to new tables and you want to create migrations and do update it to existing database but first migration will create all queries for old and new tables if you run update-database you will get "There is already an object named ['EntityName'] in the database." for your initial migration.
How fix it?
Delete all migrations and snapshot in you database project
Delete __EFMigrationsHistory table in your existing database (if exist)
Run in Package manager console:
Note before run: This code will create new context and models of your existing database to Data folder so don't forgot to check you have not Data folder in your project.
Scaffold-DbContext "your connection string"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data
Run in Package manager console:
Note before run: Create first migration for initial database with Data folder context (OldDataBaseContext is inside of your Data folder that created by step 2)
Add-Migration initial -Context OldDataBaseContext
Delete all code in Up method inside of created initial migaration in step 3
Run in Package manager console:
Note before run: Update databse with Data folder context (OldDataBaseContext is inside of your Data folder that created by step 2)
Update-Database -Context OldDataBaseContext
Delete data folder that created in Step 2
Go to snapshot and initial migration classes and change the deleted context from Data folder to main context that is exist in your database project (just fix it for build)
Run:
Note before run: Add migration for main context that have new database changes
Add-Migration newUpdate
Run:
Update-Database
I Hope this help someone.
If you are strongly believe, that new (EF7) database schema will match your old database schema (including index and key names) - you may run 'Initial' migration to empty (temporary) database and then copy __EFMigrationHistory table from there to your real database.
Otherwise, I recommend you create empty database using migration and use sql insert into ... select commands to copy data from your old database. Without this, you will receive exceptions when upgrading database later - for example changing index will lead to DropIndex and CreateIndex migration commands, and DropIndex will fail because there is no index with this name (index exist with other, pre-EF7 name).
In my projects - old (from EF6) and new database schemes are different, and I used second option.
We have entity framework migrations that create a few tables to setup an existing database. We ran the migrations once and they all ran but after looking at the database via SQL Server non of the tables that were supposed to be created were visible, even the Migration History table is not there.
We keep running the migrations again to see if we can create the tables but we keep getting the "No Pending Explicit Migrations" message.
Is there some other cache Entity Framework uses to figure out if Migrations have already run or not. How is it picking up that migrations have run yet the migration history table is not even there.
My Execution Call:
migrate.exe DllWithMigrations.dll /startUpConfigurationFile:"My Path\Web.config" /connectionProviderName:System.Data.SqlClient /connectionString:"My Connection String"