How apply Entity Framework Migration on production Server - entity-framework

I am working with Entity Framework code first.
I have managed to work with migrations, but i need to type commands in the Package Manager Console.
(for example Update-Database command).
It works fine, but it works on my developpemnt computer.
Now, imagine i have a lot of production server. Some of those servers are still in database version 1, others in version 3 and the lastest version is 5.
Is it possible to run the equivalent of Update-Database Command from C# Code ?

DbUp is one other open source tool which is effective and provides robust features to perform production DB upgrade in EF Code first approach.
Here is an informative article comparing the Dbup open source tool vs EF migration feature.

Yes,you can do that.You have to change your web.config file's connection string according to your production server and then run :
PM> Update-Database
Update : to generate scripts.
PM > Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration:
AddPostAbstract
You can refer this for more info : Getting a SQL Script

Related

How to downgrade/rollback migration in Universal Windows Platform with Entity Framework using Package Manager Console?

I modified a property in the model and it turned out it is not supported by the version of SQLite. This is the error. {"SQLite does not support this migration operation ('DropColumnOperation'). For more information, see http://go.microsoft.com/fwlink/?LinkId=723262."}
So I decided to roll it back by using the command Update-Database Migration "MyFirstMigration" but an error shows up on the console which states Update-Database shouldn't be used with Universal Windows Platform apps. Instead, call DbContext.Database.Migrate() at runtime.. This code recommended is already the code executed at the first run of the application that generates the first error above.
I am really going into circle here. Can someone suggest how I can rollback/downgrade MySecondMigration ti MyFirstMigration?
Can someone suggest how I can rollback/downgrade MySecondMigration ti MyFirstMigration?
For the same DbContext, just execute Remove-Migration command on your package manager console, it will remove the last migration of this DbContext. It is your MySecondMigration which will be removed and only MyFirstMigration left.
Update-Database shouldn't be used with Universal Windows Platform apps. Instead, call DbContext.Database.Migrate() at runtime
For this, just as this error shown, DbContext.Database.Migrate() applies any pending migrations for the context to the database, include what Update-Database did (Updates the database to a specified migration). By testing on my side, for the same DbContext, every new migration is the updating based on the old one. DbContext.Database.Migrate() will apply all the migrations for updating. If you don't want the latest update, just remove it for roll back.

asp core ef migrations on production server

Can't perform migrations on production server.
Command "dotnet ef database update" works on my computer but fails on production
Steps i tried are:
1. Fill in checkbox execute code first migrations in Visual Studio before publish.
2. dotnet ef database update not working . I installed .NET SDK but it doesn't have libraries needed.
Any suggestions appeciated.
There are a couple options:
Generate a SQL script using dotnet ef migrations script and run it on your production database.
Call dbContext.Database.Migrate() at runtime during application startup (but be careful when running multiple apps/database clients)
Also, in the next release (1.0.0-preview3) of Microsoft.EntityFrameworkCore.Tools, we'll be shipping ef.exe which you can point directly to assemblies (instead of project.json files) to perform migrations.
You can generate a migration script by running the following command:
Script-Migration -From older_migration_name -To newer_migration_name -Context ApplicationDbContext
The script will have a random name and will reside in the following path:
<Your_Project_Drive>:\<Your_Project_Folder>\<Your_Project_Folder.Model>\obj\Debug\netcoreapp3.1\<Some_Random_Name>.sql
Now just run that script on the targeted DB of production server.

How can I run an Entity Framework 7 migration on a remote database using Powershell?

In a standalone entity framework 7 project (note, not a MVC project with an entity context where the command DNX might be used), how can I run a migration on a remote database using Powershell?
I am currently using Entity Framework 7.0.0-rc1-final with Visual Studio 2015 (14.0.23107.0).
At the moment there is no way you can use pure PowerShell to do this because a utility like migrate.exe does not exist yet and importing the EF PS modules is not possible as they require a Package Manager PowerShell Host.
Here are some ideas how you can update a remote db in EF7:
One thing you could do is use the package manager console commands from within VS as usual to update the remote db. You can create a second context that has the remote db connection string and use the update-database command specifying the context to use. These commands require the following package in EF7:
https://www.nuget.org/packages/EntityFramework.Commands/.
I have done this successfully in a class lib project.
Another solution would be to use DNX commands by creating a DNX project instead of a classic one. DNX projects are not just for web sites, it is just another type of project. Here is a link that shows how to create a console app DNX project:
http://docs.asp.net/en/latest/dnx/console.html.
So with this type of project you can use the provided DNX commands that you seem to be aware of.
I hope this helped. Maybe we can give more help if you describe your situation and your end goal in more detail.
Answer too long as a comment, so adding it here...
Have you looked at this article and the links in the answer?
From that answer
The problem with importing the module into a PowerShell console is that I believe the module expects to run in a context where it has a Visual Studio DTE object available. That environment is the NuGet Package Manager Console. This issue has been brought up before. Check out this blog post and this SO question.
This blog post shows how to write code that does migrations.
What might be helpful for readers of this question is what you have tried, what is not working, and other information that might help solve your problem.

How to update database driven Code First EF model?

I have an EF code first model generated by reverse engineering an existing database - one of the supported core scenarios for EF 6.
I now have updates to the DB and I want to reflect those in the model, but I simply cannot find a mechanism to update the generated model. In the "old" EDMX world, I could update model from database, but I cannot see how to do this in VS 2013 with EF 6?
I have tried to run a migration against the new database but no changes were made to the POCOs.
To update your database in code first projects, do the following:
Find out the name of the database context by checking your source code, in the following steps I assume it is ConfigDbContext
Open PM console via menu Tools -> NUGET Package Manager -> Package Manager Console
Type the following: PM> add-migration nameofmigration -context ConfigDbContext PM> update-database -context ConfigDbContextNote: Replace nameofmigration by any unique name of your choice so you easily remember the changes. The migration code will be named with a timestamp and this name.
If you got a success message, open SQL Management Studio, connect to your database, or, if you have it already open, refresh and review the changes.
Note: If you're getting a message that you should update EF core tools, here is how you can do that. For example:
PM> Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.2.0
Replace the version number 2.2.0 by the newer version you get displayed in the warning.

How to view the EF Code First Migrations changes before calling 'Update-Database'

I am using Code First Migrations Beta 1.
I would like to see the Migrations and SQL that will run before I actually call 'Update-Database' on the Package Manager Console.
Is there a command to get this information on the Package Manager Console?
Take a look at the link below and search for the section headed Generating an SQL Script.
ADO.Net Team Blog
Run the Update-Database –TargetDatabase:"database" –Script command in Package Manager Console