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.
Related
I am setting up an Azure Release Pipeline and I need to execute any pending DB Migrations as part of the release.
I have been scouring the internet for over an hour and everything I can find is about dotnet Core, while the database is EF6 on .Net Framework, not dotnet Core (I've done this several times before for Core).
The problem, as I see it, is that EF6 works using Visual Studio's built in Package Manager Console - This just doesn't exist in an Azure Pipeline; It's a Visual Studio weirdness.
There seems to be several ways I can skin this cat, in my head, but I can't figure out how to start with either of them within the context of the pipeline...
OPTION 1: Run the Migrations on the Pipeline - but... how?
OPTION 2: SQL Scripts - Requires running the Package Manager to generate them so they can be run (if I could do that on the pipeline then I'd just run it anyway so these would have to be created locally and committed with the code which is somewhat backward as a solution IMO)
OPTION 3: Write a console app - Do I really have to do this??
You can try Entity Framework Migration Extensions.
This task allows a Build / Release to provide database connection parameters and execute an Entity Framework 6 migration against the database.
Build your project to an output folder and include the migrate.exe executable that comes with Entity Framework 6.
Create an automated build that packages up your files and makes them accessible during a Release.
Create a Release definition for the relevant Build
Add an EF6 Migration task. Once that task is added to an environment within the release, you'll need to enter the appropriate parameters to configure it. All file path parameters should be within the file system for the build, none of them are for TFS source control paths.
You can also check this article.
The answer here is to use the ef6.exe command line tool and make sure it gets shipped with your build.
This could be useful to anyone here until Microsoft update the non-existent docs: http://github.com/dotnet/EntityFramework.Docs/issues/1740 - This contains a table with a kind of translation matrix between the two.
I am working on an application that utilizes a database that often has tables added to it or modified. Is there a way I can regenerate the .edmx file as a build step or during compile time to add these new tables/modifications without manually running the wizard?
You can try to run sql scripts to insert/modify tables during build process.
Related extension:
ExecuteSqlScript
Run SQL Server Scripts Task
Or directly use PowerShell to Execute .SQL Files from Directory.
Reference below articles to change the database during build/release:
Build and Release Process for SQL Server Database
Scripts using Online TFS
Continuous Deployment of SQL Server Database Changes using Visual
Studio & TFS Release Manager
UPDATE:
Choosing the Update Model from Database is the best method for updating your EDMX. There are certain properties that don't get updated on the Conceptual layer.
See How do you update an edmx file with database changes?
Seems there isn't a good way to achieve that, however if the actions can be executed in command line, then we can add a step to run the command or script.
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
I have set up continuous integration for my project with Visual Studio Online build definitions.
When it comes to deploying my database (to an Azure test environment) I just build my SQL Server Database Project with the right publishing settings.
But I want to switch to Entity Framework's code first approach and leverage the migration feature, which requires me to call migrate.exe.
My question is - how could I run migrate.exe from VSO build definitions?
We've succesfully implemented an automated EF code first migration at deploy time on top of TFS Build vNext in the following way:
It basically involves 3 steps (per EF-context):
Copy EF-project to a staging directory
Copy migrate.exe in same folder (migrate.exe requires to be placed right next to assembly containing EF migrations)
Execute migrate.exe
In detail:
Copy Files "task"
Source folder: $(build.sourcesdirectory)
Contents: Contoso.EF\bin\debug\ **
Target folder: $(build.artifactstagingdirectory)/EF
Copy Files "task"
Source folder: $(build.sourcesDirectory)\packages\EntityFramework.6.1.3\tools
Contents: migrate.exe
Target folder: $(build.artifactstagingdirectory)\EF\Contoso.EF\bin\debug\bin\debug
Batch script "task"
Path: $(build.sourcesdirectory)_Deploy\MigrateEFContext.bat
Arguments: $(build.artifactstagingdirectory)\EF\Contoso.EF\bin\debug Contoso.EF.dll [SQL-SERVER-INSTANCE] [DbName] System.Data.SqlClient
The MigrateEFContext.bat file assembles the migrate.exe-command with its arguments:
SET EFDir=%1
SET EFContext=%2
SET connStringDataSource=%3
SET connStringInitialCatalog=%4
SET connectionProviderName=%5
%EFDIR%\migrate.exe %EFContext% /ConnectionString:"Data Source=%connStringDataSource%;Initial Catalog=%connStringInitialCatalog%;Integrated Security=true" /connectionProviderName:%connectionProviderName% /verbose
I assume you are using vNext build, add a "Nuget Installer" task in your build definition first to restore the Entity Framework during the build. Migrate.exe will be installed in \packages\EntityFramework.\tools folder. Then add a "Command Line" task to run the migrate.exe. Enter “\packages\EntityFramework.\tools\migrate.exe" in "Tool" area and the arguments in "Arguments" field.
You can also look at executing your migrations at App startup time.
add the following to your Application_Start() event in global.asax
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();
This will fire the migrations at each application startup. you could also wrap with conditional logic to control how it is fired.
I'm trying to setup SQL Server CE 4.0 as my Test Database in order to run integration tests, as described in this article: http://lostechies.com/erichexter/2013/01/06/using-sql-compact-for-integration-tests-with-entity-framework/
These are all the projects in my solution:
UnitTests
Website
DataAccessLayer
Core
I added a SQL Compact CE 4.0 reference and connectionString to the app.config file on the UnitTests project and then tried to run the update-database command:
update-database -ProjectName DataAccessLayer -StartupProjectName UnitTests -Verbose
This works fine for most migrations but it stops short of finishing and then I get this error:
Direct column renaming is not supported by SQL Server Compact. To
rename a column in SQL Server Compact, you will need to recreate it.
Is there a way that I can turn off migrations and just re-create the entire database JUST for the unit tests projects?
Found it. Just had to set the Database initializer on the unit tests project:
System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
I call this from the FixtureSetup method on my unit test project.