Can I do a dry run of Update-Database in EF 6? - entity-framework

I am wondering if there is a way to run Update-Database from the Package Manager Console with something similar to the Rails --dry-run flag, in order to see what will be run before doing it. My googling has led to no corresponding command for EF.
Is there one? Is there a hack to do essentially the same thing?

You could use Update-Database with a -Script flag to generate a SQL script instead of applying the migration. If you don't specify a source migration, it will use the current database state as the starting point, so you can use the file to verify what would have been executed on the database.
https://msdn.microsoft.com/en-us/data/jj591621.aspx#script

Related

How to use `Scaffold-DbContext` command in VS Code

I have been trying to perform scaffolding on my existing database SQL Server in VS Code software so as to create DBContext and entity domain models. However in VS Code the only available commands are Add Package and Remove Package, I need to run following command in Nuget Package Manager Console.
Is there any other way to do scaffolding in VS Code.
I am running Dot Net Core 3.1 and EF Core 3.1.3. Database is SQL Server and Writing WEB Api using C#
For this to work you will need to add following package to vscode using Nuget Package Manager: Add Package command from commandpalatte
Microsoft.EntityFrameworkCore.Tools.DotNet (2.0.3 or latest)
Then do
dotnet restore
Then you need to do following command in terminal window of vscode in directory where your csproj file is, -o specifies the output directory where created domain entity models and dbcontext will reside. Suggest you to create a folder beforehand.
dotnet ef dbcontext scaffold "<your existing db connection string>" Microsoft.EntityFrameworkCore.SqlServer -o Models
if anyone yet using this thread and If not using Visual Studio as an IDE then you can create, scaffolding globally using
EFCore CLI Tool can be installed by using global dotnet-ef tools.
The CLI tools are cross-platform and easy to use.
Run below command on Command Prompt,
dotnet tool install --global dotnet-ef
Followed by adding below package using CLI (Please use the project location directory to run this command)
dotnet add package Microsoft.EntityFrameworkCore.Design
Once installed successfully, please verify the installation by running below command as below,
dotnet ef
You are all set !!
Please run the Scaffold-DbContext command to generate required scaffolding as explained above.
For More Please read this forum

dotnet ef command no longer works after upgrading to Visual Studio 16.3.0

This happened at home first, so I thought maybe it was an issue with my desktop PC at home. But now that I am back at work, I tried the upgrade and got the same thing.
Screenshot before upgrade
Screenshot after upgrading Visual Studio
The error I get is:
Could not execute because the specified command or file was not found.
Possible reasons for this include:
You misspelled a built-in dotnet command.
You intended to execute a .NET Core program, but dotnet-ef does not exist.
You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Any ideas on why this happens? And how do I get back the dotnet ef command. I mean must have been in the $PATH previously, otherwise it wouldn't have worked before.
This is a breaking change in Entity Framework Core 3.0:
The EF Core command-line tool, dotnet ef, is no longer part of the .NET Core SDK.
...
Starting in 3.0, the .NET SDK does not include the dotnet ef tool, so before you can use it you have to explicitly install it as a local or global tool.
You need to install the Entity Framework Core Tools. To install it globally, run this on the command line:
dotnet tool install --global dotnet-ef

How to get SQL Script using Entity Framework Core(code first)

I'm create a web app using Asp.net core with EntityFramework core, but the Update-Database command no longer has the "-Script" switch. How can I get the sql script? Thanks.
Run this command to produce the SQL Script
> dotnet ef migrations script -o fileName.sql
Also there's a powershell command for it too: powershell#script-migration
I come at this question to find command that can generate script for all tables/db objects from dbcontext. If you are also looking same instead of just migration or update, command is :
dotnet ef dbcontext script -o filename.sql
Just open the command prompt in folder in which entity framework exist & run this. this will create SQL file in same folder.
Just make sure you have dotnet ef tools installed, if not already use
dotnet tool install --global dotnet-ef
before the above context command.

How to run Code-First Migrations from a psake build?

I can type Update-Database, Enable-Migrations etc, from Package Manager Console and it works fine.
If I need to do the same from a regular powershell session, or in a psake build file, then how do I do it?
I tried importing the module EntityFramework.5.0.0\tools\EntityFramework.psm1 from the packages directory, and I did get the Update-Database, Enable-Migrations functions, but I cannot supply their arguments - they need a project, source, and 6 more - and there is no documentation whatsoever. Can I not automate the database deploy on some machine in our CI chain ?
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.
Use migrate.exe from EntityFramework NuGet package.
https://stackoverflow.com/a/14139229/991267

EF 4.3 Migration - how to produce a downgrade script?

I have an issue which I could not find answer for across the web.
I am using CodeFirst EF 4.3.1 Migrations with MsSQL.
I have added several migrations and now I want to produce a script for upgrade/downgrade between two migrations.
For upgrade I run the following command which successfully reproduces an upgrade script:
PM> Update-Database -Script -SourceMigration:"201205161144187_AddPostAbstract" -TargetMigration:"201205161203310_BlogLimitsAndTableRename"
However, for downgrade I run the following command which fails with the following error:
PM> Update-Database -Script -SourceMigration:"201205161203310_BlogLimitsAndTableRename" -TargetMigration:"201205161144187_AddPostAbstract"
Scripting the downgrade between two specified migrations is not supported.
Any ideas how can I generate a downgrade script?
Thanks.
It looks like migration API expects that you want to do downgrade only from "last version".
If BlogLimitsAndTableRename is your most recent migration (the last applied) you can simply run:
Update-Database -Script -TargetMigration:"201205161144187_AddPostAbstract"
If it is not your last migration you need to revert your development database to it first:
Update-Database -TargetMigration:"201205161203310_BlogLimitsAndTableRename"
and now you should be able to use the first command to get a script.