Running dotnet ef database update on tfs build agent - tfs

I'm trying to make a CD setup which migrates my target database on release in TFS 2017 (update 3). My artifacts only contains my website assemblies, with DB context and controllers etc. If I update my database during build (and not during release) I can run
dotnet restore
dotnet ef database update
and it works nicely, but this ofcourse does not work during release, as the source code is not available in the artifacts, and dotnet restore requires the .csproj file.
Anyone have suggestion of how to set up migrations during release? Do I need to include my source in the artifacts?
I'm using .net core 2.0 and ef core 2.0

If you run dotnet ef database update with the --verbose option, you'll see a call to dotnet exec ef.dll. This is the command you can use directly on the binaries without needing the source code.

Related

Dockerfile dotnet restore locally instead of reaching to nuget server

I'm working on a .net core 3.1 project for a company. I have a pretty simple dockerfile configured that leverages RUN dontnet restore to retrieve all the necessary nuget packages for my project. This works fine for me. It will build and run the container no problem.
The problem comes when the company pulls my solution to their network and they try to run the same dockerfile. Their firewall rules block SSL connections to external sites (with explicit exceptions that nuget is not part of). So when they try to build the container they get failure when the restore tries to access "https://api.nuget.org/v3/index.json". Which makes sense to me for the reasons above.
If I update my dotnet build to include nuget packages as part of the published output, can I have the dockerfile update to reference that published output for nuget packages instead of doing a dotnet restore from nuget itself? Essentially is there a good way to handle the docker build process so it can be done offline (i.e. without relying on external sources)?
You could create a local cache using NuSave, then add it to your Docker container; which can be used to restore the solution offline.
As #Lex Li mentioned above, the better way to do things was a self contained publish with dotnet and then copying those files over into the container so that there was no need to do a dotnet restore.
I typically like to do dotnet restore to keep the size small but in this case this was the right way to do it.

Where is nuget.exe in the .net core docker SDK

I'm using the mcr.microsoft.com/dotnet/core/sdk:3.0-buster image to build a docker image, but I need to add the Telerick nuget feed source. I can pass in a nuget.config file and it will work, so I know nuget itself is working when the dotnet restore operation runs.
Nuget must be in that image somewhere, but apparently not in the path. So when I try to run this in my Dockerfile:
RUN nuget.exe sources add -Name telerik -Source https://nuget.telerik.com/nuget/ -username myusername -password mypassword
I get the error:
/bin/sh: 1: nuget.exe: not found
Where is nuget.exe in the dotnet core docker image?
The nuget CLI itself is not included with the .NET Core SDK itself.
The .NET Core SDK includes a subset of nuget functionality as built-in. For other functionality, you have to fall back to workarounds.
Telerik docs point out that you have to put your configuration in the Nuget.config file in .NET Core.

How to UPDATE DATABASE in Code First approach automatically after check-in and Publish code in Continuous deployment

​In our Web API application, Continuous deployment need a following scenario.
User will check in code in VS, Code will get automatically build, Code will be Published, Code will be deployed.
But If we are using Entity Framework Code First approach, How can we update database without manual commands (Add-Migration/Update Database)and make database up to date with that check-in.
You can try to run Add-Migration/Update Database commands in the build/deploy process.
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.
Reference this thread : How can I run Entity Framework's migrate.exe from Visual Studio Online?
You can also try the extension "Entity Framework Migrations" which contains a set of tasks which allow you to work with Entity Framework code first migrations:
Method 1: Generating SQL script
The first method allows you to generate a SQL script containing all
migrations. This script can be obtained by manually running
Update-Database -SourceMigration 0 -Script in the NuGet package
manager console in Visual Studio. You can then either manually run
this script after the release or automatically during the release
using a extension that allows you to run SQL scripts.
Task name: Generate migration SQL script
Other articles may helps:
Deploying an Entity Framework Database into Production
Using TFS Build to Deploy Entity Framework Database Migrations with
Migrate.exe
Continuous Delivery with TFS: Our Sample Application

TFS Build missing Castle Windsor

I used NuGet to add CastleWindsor to a project. Eveything works ok.
When I check it into tfs, I get the following message.
Unable to find version '3.3.3' of package 'Castle.Core'.
Any idea how I can get the build server to get the new version of Castle.Core?
First just as Dave commented, please check if you have add the nuget install task in your build definition and before your build task.
Also make sure you are using the right version of Nuget. For example, if you already use V3.0 and the config file are still point to V2.0. You will get this error.
Moreover, double check if the packages can be restored successfully on you dev PC and build agent manually, you can also compare the nuget.config file on your TFS server and dev PCs to see if there is any difference between them. The nuget.config file locates at "%APPDATA%\NuGet\NuGet.Config".
TFS2012 does not restore the nuget packages automatically, you need to add a build step to call the nuget command to restore the nuget packages. Refer to this link for details: Package Restore with Team Foundation Build.
With TFS 2013 and later, packages are automatically restored by
default during build, provided that you're using a Team Build Template
for Team Foundation Server 2013 or later.
If you're using a previous version of build templates (such as in a
project that's been migrated from earlier versions of TFS), you'll
need to also migrate those build templates to TFS 2013. This
essentially means recreating the custom parts of the Build Templates
using the appropriate template for your source control (TFVC or Git).
For earlier version of TFS, you can simply include a build step to
invoke command-line restore as described earlier.

Problems Building .Net Core on TFS

I have a solution containing multiple .Net Core projects, some of which are dependent on other projects in the same solution. The whole solution is hosted in a Git repository of an on-premises TFS server.
I want to set up an automated build process on the TFS server. For lack of better knowledge, I use the Visual Studio build template and leave all the settings on their defaults. When I run the build process, it fails on the "Build solution" step with the error message
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets(262,5): error : Project MyProject does not have a lock file.
Please run "dotnet restore" to generate a new lock file. [C:\TFSBuildAgent_work\6\s\MyProject\MyProject.xproj]
So I go ahead and create a PowerShell script with the content dotnet restore that I run before "Build solution" but after "Nuget restore". This also fails, this time on step "Dotnet restore" with the error message
Errors in C:\TFSBuildAgent_work\6\s\MyProject\project.json
Unable to resolve 'ProjectDependency (>= 1.0.0)' for '.NETFramework,Version=v4.6.1'.
When I run dotnet restore on my local machine, it completes without any issues.
Any help or hints are greatly appreciated!
I helped myself now in packaging the project dependency and uploading it to my inhouse package server. dotnet restore will then pull it from the server and build the whole project. Not the nicest way, but at least it's working.

Resources