Run EF Migrations with Azure DevOps - c#

I want to run EF migrations from Azure DevOps.
For dev environment I use update-database in Visual Studio/Package Manager Console but in DevOps the only option I found is to generate a migration script, script that is corrupt when I have SQL code in my migrations like this one:
migrationBuilder.Sql(#"
CREATE FUNCTION [dbo].[MyFunc]
(
#param2 int,
#param1 int
)
RETURNS FLOAT
AS
BEGIN
DECLARE #result AS FLOAT
--content
RETURN ISNULL(#result, 0);
END");
I tried also with some extensions but without success.
https://marketplace.visualstudio.com/items?itemName=bendayconsulting.build-task&ssr=false#overview
The command I use now in DevOps is:
migrations script --startup-project $(Build.SourcesDirectory)/Test.Web/Test.Web.csproj --project $(Build.SourcesDirectory)/Test.DAL/Test.DAL.csproj --context TestContext -i -o migration.sql
Can I bypass the script generation and directly apply the 'update-database' command in DevOps?

Related

Unrecognized option '-Context' when running dotnet ef command

Starting a new project, and trying to set up the database for authentication. I run the following command in powershell.
dotnet ef database update -Context ApplicationDbContext
and get the following error Unrecognized option '-Context'
So I try running it without the -Context option just to see and get:
More than one DbContext was found. Specify which one to use. Use the '-Context' parameter
for PowerShell commands and the '--context' parameter for dotnet commands.
I also tried running it with --context instead of -context, but get the same error. Any suggestions on why it recognizes that I need that option, but at the same time tells me it doesn't recognize the option?
I also restart powershell.
Instead of using --context ApplicationDbContext have you tried -c ApplicationDbContext
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#aspnet-core-environment
I was having the same problem and instead of using dotnet ef database update -Context ApplicationDbContext, I replaced -Context with -c which worked for me.

Using a DB File to run Tests on Azure Devops

Is there a way to create a DB File on the Azure Devops pipeline using one of the tasks?
My line of thought is Create a localdb on the agent (Using VS) and run the unit tests (SSDT) on that DB file like I do with VS. I can create db file Tools>connect db> Sql server db file and putting in a name. I can connect to it and run the tests. It seems like I cant do this on Azure devops pipeline.
I know the preferred way is to allocate an Azure SQL server and run the tests against those but the DB is very small and if i can run those against the db file it seems like a better idea.
Basically I found a way to do it all on the agent. However, the agent localdb has to be updated if you are using newer syntax.
- task: CopyFiles#2
inputs:
Contents: '**/Output/*.dacpac'
flattenFolders: true
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
sqllocaldb start MSSQLLocalDB
sqllocaldb info MSSQLLocalDB
#import SqlServer module
Import-Module -Name "SqlServer"
# create variable with SQL to execute
$sql = "
CREATE DATABASE [MyDatabase]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'MyDatabase', FILENAME = N'd:\a\1\s\testing.Data\test\bin\Output\MyDatabase.mdf' , SIZE = 1048576KB , FILEGROWTH = 262144KB )
LOG ON
( NAME = N'MyDatabase_log', FILENAME = N'd:\a\1\s\testing.Data\test\bin\Output\MyDatabase_log.ldf' , SIZE = 524288KB , FILEGROWTH = 131072KB )
GO
USE [master]
GO
ALTER DATABASE [MyDatabase] SET RECOVERY SIMPLE WITH NO_WAIT
GO
ALTER AUTHORIZATION ON DATABASE::[MyDatabase] TO [sa]
GO "
Invoke-SqlCmd -ServerInstance "(localdb)\MSSQLLocalDB" -database master -Query $sql
- task: SqlDacpacDeploymentOnMachineGroup#0
inputs:
TaskType: 'dacpac'
DacpacFile: '$(Build.ArtifactStagingDirectory)/*.dacpac'
TargetMethod: 'connectionString'
ConnectionString: 'Data Source=(localdb)\.;Initial Catalog=MyDatabase;Integrated Security=True;'
With this you can attach a created .mdf file that was generate to your localdb and publish your dacpac to it. Then if you want to run your tests, you can do so.
Using a DB File to run Tests on Azure Devops
As workaround, you could try to check-in the LocalDb files (mdf and ldf), copy the files to output and change the connection string to use the current execution path:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer($”Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=Contacts;AttachDbFilename={AppDomain.CurrentDomain.BaseDirectory}Core\\IntegrationTests\\Contacts.mdf;Integrated Security=True”);
}
You could check the document Integration Testing with SQL LocalDb on your build server for some details.
Hope this helps.

How to perform migrations in the package manager?

I installed FluentMigration to manage my SQL files.
In the package management I execute the following commands:
PM> dotnet add package FluentMigrator
PM> dotnet add package FluentMigrator.Runner
Migration
[Migration(201805041513)]
public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
{
public override void Up()
{
Create.Table("Pessoa")
.InSchema("angularCore")
.WithColumn("Id").AsInt32().Identity()
.WithColumn("Nome").AsString(80)
.WithColumn("SobreNome").AsString(50)
.WithColumn("Email").AsString(50)
.WithColumn("IdTpoPessoa").AsInt16()
.WithColumn("IdEndereco").AsInt16();
}
}
Out-of-process (for some corporate requirements)
PM> dotnet tool install -g FluentMigrator.DotNet.Cli
Error:
No executable found corresponding to the "dotnet-tool" command
Documentation
Edit
Run in
PM> dotnet tool install -g FluentMigrator.DotNet.Cli
PM> dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"
Generate teste.db
In the old versions you run the migrations directly in the database, I
did not understand how to update my database, ie create the Person
table through the generated test.db file?
I was able to get this to work without any issues. Here is what I did:
First I installed .NET Core 2.1-Preview 2. After installing I verified the version:
dotnet --version
2.1.300-preview2-008533
I then created the project
mkdir testfm
cd testfm
dotnet new console
Installed the nuget packages
dotnet add package FluentMigrator
dotnet add package FluentMigrator.Runner
dotnet add package FluentMigrator.Runner.SQLite
dotnet add package Microsoft.Data.Sqlite
Installed the CLI tool
dotnet tool install -g FluentMigrator.DotNet.Cli
Created a Migration Class called Migration1.cs
using FluentMigrator;
namespace test
{
[Migration(201805041513)]
public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
{
public override void Up()
{
Create.Table("Pessoa")
.InSchema("angularCore")
.WithColumn("Id").AsInt32().Identity()
.WithColumn("Nome").AsString(80)
.WithColumn("SobreNome").AsString(50)
.WithColumn("Email").AsString(50)
.WithColumn("IdTpoPessoa").AsInt16()
.WithColumn("IdEndereco").AsInt16();
}
}
}
Compiled the project
dotnet build
Ran the migration from the root project directory
dotnet fm migrate -p sqlite -c "Data Source=test.db" -a
".\bin\Debug\netcoreapp2.1\test.dll"
I then Received the following messages.
I then confirmed the table was created by viewing the SqlLite DB
To run this same migration on a Sql Server 2016 you would run:
dotnet fm migrate -p SqlServer2016 -c
"server=SQLSERVERINSTANCE;uid=testfm;pwd=test;Trusted_Connection=yes;database=FluentMigrator"
-a ".\bin\Debug\netcoreapp2.1\test.dll"

ApplicationDBContext AspUsers Migration command

I'm attempting to setup store for my IdentityServer4 Token server,
I'm following along with this tutorial where I encountered database migrations like so:
dotnet ef migrations add InitialIdentityServerMigration -c PersistedGrantDbContext
dotnet ef migrations add InitialIdentityServerMigration -c ConfigurationDbContext
dotnet ef migrations add InitialIdentityServerMigration -c ApplicationDbContext
Apparently I'm running a different version of powershell, or the tools are messed up so I have to run my migrations using a different syntax
Add-Migration InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
Add-Migration InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
Theese two commands worked great however I'm still missing the third command to generate the ApplicationDbContext:
When I run I get this exception:
SqlException: Invalid object name 'AspNetUsers'.
I'm missing this table along with a few others from the database for Identity, does anyone know which Migration to use?
If you create the default ASP.NET project with authentication, you will see a migration file 00000000000000_CreateIdentitySchema.cs.
So
dotnet ef migrations add CreateIdentitySchema -c ApplicationDbContext
should work.
It turns out it was simple. I though Migrations were generating the Db from something that it had in code, but in actuality it seems to be constructing the database based off of the CLR Class configuration
Add-Migration InitialStillDontKnowWhatThisNameIsFor -c ApplicationDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
I don't know what the names is for but that will generate the DB

Visual Studio Code Entity Framework Core Add-Migration not recognized

I've used yoman to generate an ASP.Net Core Web API application via the Visual Studio Code Editor. For reference, I followed this tutorial here.
The API works fine. However, I am trying to use EntityFramework Core Migrations with SQL Server. When I type the following into the Visual Studio Code Terminal:
Add-Migration MyDbInitialMigration
I get the following message:
'Add-Migration' is not recognized as an internal or external command, operable program or batch file.
I have the Microsoft.EntityFrameworkCore.Tools: 1.1.0-preview4-final dependency installed. I did this using the .Net Core Project Manager (Nuget) extension.
In Visual Studio 2015 this command works fine from the Package Manager Console.
I assume that using Visual Studio Code's Terminal is the problem. But does anyone know how I can use EF Core Migrations from within the VSCode editor itself?
Solution
Running the dotnet ef migrations add InitialCreate command yielded the following error:
No executable found matching command "dotnet-ef"
To solve this I needed to install the following dependency, And add it to the tools section:
Microsoft.EntityFrameworkCore.Tools.DotNet
The correct format to add a new migration is:
dotnet ef migrations add yourMigrationName
and to update database is:
dotnet ef database update
You need to add:
dotnet tool install --global dotnet-ef
Im working on Mac, so Ruby is installed by default. My EF commands required lots of extra parameters --project, --startup-project etc. This was a pain to type every time, so I used rake to make this easier.
In my project root, I added a file called rakefile with these contents:
desc "Add Migraion"
task :'add-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations add " + ARGV[1] + " --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj "
end
desc "Remove Migraion"
task :'remove-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations remove --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
desc "Update Database"
task :'update-database' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef database update --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
Then at the command line, I run these commands:
rake add-migration <migrationName>
rake remove-migration
rake update-database
First we need to add reference in *.csproj file in the following way
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />
</ItemGroup>
in Bash/Command prompt
dotnet restore
after that
dotnet ef migrations add MyDbInitialMigration

Categories

Resources