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

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

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

Microsoft NuGet targets Error while building Project from Teamcity

I attempted in building the .NET Standard Library Project using .NET Framework 4.6.1 from Command prompt using MSBuild Commmand. I manage to succeed building the project from command prompt.
I tried the same MSBuild command to build the project from Teamcity Command line build step, but ended up getting the following error:
C:\Program Files (x86)\MSBuild\Microsoft\NuGet\Microsoft.NuGet.targets(140,5): error : The package Microsoft.NETCore.Portable.Compatibility
with version 1.0.1 could not be found in C:\Windows\system32\config\systemprofile\.nuget\packages\. Run a NuGet package restore to download the package.
I tried Restoring nuget.exe restore mysolution.sln but noting works.
Please help me to compile the solution from Teamcity command line step. Its strange to get compiled from command prompt but not from teamcity command line.
Looks like there is a known issue in NuGet when restoring packages using the LocalSystem account wherein the packages cannot be accessed under the C:\Windows\system32\config\systemprofile\.nuget\packages folder, even though account permissions should allow it (see this GH issue). The current recommended workaround is to use the NUGET_PACKAGES environment variable to specify another location for the packages (e.g. C:\NuGet\packages).

How to use paket from command line

I installed paket from nuget in Nuget Package Manager Console with:
Install-Package paket
I then tried to run paket convert-from-nuget. It stalled out on a user prompt (it wouldn't let me type into the package manager console). My next thought was to run it from command line, but how to do so is not documented.
Just putting paket convert-from-nuget into a standard dev command prompt results in an error saying "paket" is not recognized.
How do I run paket from the command line or powershell, and how do you specify which solution to work against?
The way to setup paket in your repository is as follow:
1 Download a release of paket.bootstrapper.exe
This is a lightweight utility which obtains and updates paket.exe, pick stable release from official release page:
https://github.com/fsprojects/Paket/releases
2 create a .paket folder
md .paket
3 put the downloaded bootstrapper in this folder and invoke it
cd .paket
paket.bootstrapper
now you have an up-to-date paket.exe ready to ease your handling of dependencies.
4 convert from nuget
cd ..
.paket\paket convert-from-nuget
Please checkout the https://github.com/fsprojects/Paket.VisualStudio also for Visual Studio plugin to help you authoring paket.dependencies and paket.references file
Please also join https://gitter.im/fsprojects/Paket if you have any questions.
The Chocolatey package modifies the PSModulePath envivornment variable. I've observed that sometimes that modification isn't picked up until the system is restarted (or at least not until the user logs out and back in again). In the meantime, you can import the module using:
Import-Module <path-to-packages>\Paket.PowerShell\Paket.PowerShell.psd1
The packages path is usually something like C:\Chocolatey\lib. OTOH, re-reading your question, are you referring to the Nuget inside of Visual Studio? If so, that downloads from NuGet.org and that pkg puts paket.exe in $(SolutionDir)\packages\Pakget.1.18.5\tools\paket.exe. Your version number may varying.
Unfortunately the fact that PowerShell V5 introduces Install-Package (which downloads from Chocolatey by default) is going to get a little confusing vis-a-vie the NuGet Package Manager Console's Install-Package in Visual Studio.

Nuget Command-line install is not launching Install/Init scripts

I was trying to use Nuget as a software deployment system (repository, versioning and delivery) - idea from Octopus. Previously I was packaging ASP.NET sites into a self-extracting RAR archives with a .CMD startup scripts embeded. Now I'm trying to use Nuget creating puckages during automated build. The issue is that the package installation scripts (tools\Install.ps1 or tools\Init.ps1) do not execute if the package is being installed using command line:
nuget.exe install <package_id> -OutputDirectory <install_folder> -source <local_repo>
Same scripts are able to execute when package installed from Visual Studio Package Manager or Console.
I do not see why this shouldn't be possible given omnipresence of PowerShell.
Am I missing something or this is behaviour by design? Will appreciate you help.
Yes, we did consider MSDeploy but we already have install scripts that do the same thing and give more control and we need some strong package management and repository for build artifacts (something that Java folks do with Maven).
As of today, the powershell scripts are not invoked from doing installations from command line.
One reason for this is that, in general, most of the install/init actions are tied to dte and the visual studio project and doesn't add much value to be able to run it from outside VS.
We have a backlog item for enabling support for exe based scripts too in addition to powershell.