Prebuild entity framework migration for deployment - entity-framework

Currently, I run the migration using
dotnet ef database update
To do this, I have to source code with deployed to it will be able to build the project:
Example output:
Build started...
...
Is there a way to run the migrations using the project binary which I prebuild in the ci/cd process?

I think you could use the --no-build flag. For more information, see the cli reference here.

Related

Workflow for updating latest Entity Framework Core migrations automatically

So I have an EF Core project. When I need to add/migrations I issue and update command:
dotnet ef database update -v
This all works fine, if I need to update the QA, Staging, Prod database I update the connection string in my appsettings.json file and run the command.
What happens if I don't have access to production from my local machine? How do you go about updating the database to the latest migration?
If I remember working with Entity Framework (not .net core). It would try and update the database automatically when I deploy a new version of the .net application. Does this functionality still exist in .NET core?
Yes you could run
dbContext.Database.Migrate();
On startup but it is generally not a good idea to conflate database migration with your application lifecycle - best practice is to keep your application start up as fast and simple as possible because an application start failure is hard to diagnose remotely, and a migration would introduce a lot of unwelcome complexity.
The alternative is to run a migration operation as part of your deployment. It depends on what method you use to deploy but say for example you use a CI server, you will be able to run
dotnet ef database update
after copying the new code but before starting the application back up.

EF Code First Migration with Migration.exe in Visual Studio Online [Release]

Overview:
I am working on
ASP.NET MVC Project with Entity Framework's Code First
approach for the database.
I recently automated the deployment process via VSO pipelines (Build & Release)
Problem:
For Application Deployment it's perfect and working fine.
For Database I am facing issues (explained below).
Details:
I read this, this and this but issues in these solutions are
They are using Publish Profile setting, which would run the migration on application start event, which I don't want (not a best practice though)
For others using Build Pipeline (copying (a) EF tools, (b) Dll's and (c) run migrate.exe via command line to run update-database command), and it will eventually run migrations on Successful Build. I rather want it to deploy changes on 'Successful Release'.
My Work
I moved step (c) from build to release pipeline. But it's not able to find the drop.
Secondly, How to roll back in case of a crash on production?
Can anyone suggest what the best approach to solving this scenario?
In the case of crash on production, you can rollback database to previous version by running update-database -TargetMigration:"Migration_Name"

VSTest-Task not running .NET Core 2.1 xUnit-Tests from Test-plan

I'm trying to create a release pipeline in VSTS that runs my xUnit-tests as specified in a Test Plan.
Long story short: I can't get it to work.
What I'm using:
Azure DevOps (formerly VSTS)
Visual Studio Test task (v2.*)
Test project targeting .NET Core 2.1
xunit 2.4 with xunit.runner.visualstudio 2.4
In Azure DevOps I defined a Test Plan that contains a Test Suite which contains a Test that has an Associated Automation which points to my xUnit test.
I had to use the REST API to link the test code to the Test as described here.
I can select that Test in the visual designer for the VSTest task.
When I run the release pipeline the VSTest task fails with the following error message:
DiscoveryMessage : System.IO.FileNotFoundException: Unable to find tests for D:\a\r1\a\Foo.Tests.dll. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk" and framework version settings are appropriate. Rerun with /diag option to diagnose further.
The path to the Foo.Tests.dll is correct, all required files are copied as well.
I explicitly specified the framework version in a .runsettings file (as the option Other console options doesn't work when using the Test plan option).
Specified the path to custom test adapters
used Visual Studio 2017 and Installed by Tools Installer options
Added a .NET Core Tool installer to install the correct .NET Core SDK
...and any other combination of settings I could think of.
The error message is still the same.
Any ideas what I might be missing? Your help would be greatly appreciated at this point!
After several more hours we stumbled across a web page that stated that you don't have to copy the binaries of your test project as input for the VSTest task but PUBLISH it instead. That never came to mind as vstest.console.exe runs smoothly when you point it at the binaries on a local machine.
UPDATE: We had to add a Publish Artifact task at the end of our Build Pipeline and make the Release Pipeline pick up the published artifact.

Dotnet test on .NET Core 1.1 in VSTS: No Test Discoverer is Registered

I'm using VS 2017 with the new csproj in a .NET Standard class library, trying to test the library with a .NET Core 1.1 test project using MSTest testing framework with the dotnet test command. Running locally works perfectly fine; when I send the build to continuous integration, I get the error:
No test discoverer is registered to perform discovery of test cases.
How do I get this discoverer registered, and my tests running, in VSTS?
This is my build process on VSTS (detailed on my blog here)
Add a dotnet restore task.
Then a dotnet build task.
Add a dotnet test task with the arguments --no-build --logger "trx;LogFileName=tests-log.trx
Add a Publish test results task with the following settings
Test Result Format = VSTest
Test Result Files = **/tests-log.trx
Merge Test Results = (checked)
In Control Options set Run this task to run even if a previous task has failed
Refer to these steps below:
Create a new .Net Core test project with MSTest testing framework, there are MSTest.TestAdapter and MSTest.TestFramework package references, which is importance
Add .NET Core (Preview) step (Command: restore)
Add .NET Core (Preview) step (Command: build)
Add .NET Core (Preview) step (Command: test)
Queue build with Hosted VS2017 build agent.

Continuously Deploy ASP.Net Core application using PowerShell script in TeamCity

I have a ASP.Net Core .xproj. I want to deploy the website continuously on to a server which is hosting the website using IIS. I have TeamCity installed on my server and able to deploy other .NET applications using MSBuild and PowerShell scripts. But now, I want to deploy .NET Core application either using MSBuild or PowerShell or is there any other way of achieving this?
There is designated TeamCity dotnet plugin which gives you all required .NET core commands:
build,
pack,
publish,
restore,
test
For dotnet cli parameter specs look in [MS Docs] (https://learn.microsoft.com/en-us/dotnet/articles/core/tools/).
While you use xproj/project.json concept, you need to use .NET Core CLI tools like dotnet build, dotnet publish.
MSBuild has been supporting starting from .NET Core 1.0 Preview 3 and one of the main requirement is a project migration to csproj format. See Announcing .NET Core Tools MSBuild “alpha”.