question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"dotnet ef migrations bundle" => bundle.exe unexpected exit code on failing migrations

See original GitHub issue

Question

I’m using the ‘new’ dotnet ef migrations bundle command to create a bundle migration executable I package this into a docker file to run as a pre-deployment step of my release process (using argocd).

What I noticed is that when the bundle application runs into an exception like a missing connection string as an example the exit code is 1 (failure). This is expected behavior.

The thing that I did not expect is that when the bundle application starts but hits an exception in one of the migrations itself the exit code is 0 (success). As this exit code passed to the docker container exit code my pre-deployment step thinks all is good and continues the deployment.

I am wondering if I’m doing something wrong as I would expect that the main usage of this bundle application would be in a pre-deployment scenario being:

  • Build/release pipeline
  • Pre-deployment hooks using helm or argocd

Proposed solution/change

It would be nice if we could pass an exit code toggle/option to the bundle application on how to respond to failing migrations

efbundle --hard-fail-on-migration-failure

Code

Docker file

FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

ENV CONNECTION=''
ENV DATABASE_NAME=''

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Sample.Infrastructure/Sample.Infrastructure.csproj", "Sample.Infrastructure/"]
COPY ["Sample.Core/Sample.Core.csproj", "Sample.Core/"]
RUN dotnet restore "Sample.Infrastructure/Sample.Infrastructure.csproj"
COPY . .
WORKDIR "/src/Sample.Infrastructure"
RUN dotnet build "Sample.Infrastructure.csproj" -c Release -o /app/build

RUN dotnet tool install --global dotnet-ef
ENV PATH="${PATH}:/root/.dotnet/tools"

FROM build AS publish
RUN dotnet ef migrations bundle --project Sample.Infrastructure.csproj -r linux-x64 --verbose --configuration Bundle --self-contained -f -o /app/efbundle

FROM base AS final
WORKDIR /app
COPY --from=publish /app/efbundle .

ENTRYPOINT ./efbundle --prefix-output --verbose --connection "${CONNECTION}" | sed 's/{CATALOG_NAME}/'$DATABASE_NAME'/;'

Stack traces

Log output

info:    Microsoft.Data.SqlClient.SqlException (0x80131904): Reference to database and/or server name in 'samplxxxx.dbo.EVChargers' is not supported in this version of SQL Server.
info:       at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
info:       at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
info:       at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
info:       at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
info:       at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
info:       at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
info:       at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
info:       at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
info:       at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
info:       at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
info:       at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
info:       at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
info:       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.ExecuteInternal(String[] args)
info:       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.<>c__DisplayClass6_0.<Configure>b__0(String[] args)
info:       at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
info:       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.Execute(String context, Assembly assembly, Assembly startupAssembly, String[] args)
info:    ClientConnectionId:5e3b07c9-2e34-4232-9787-dce9dd15362a
info:    Error Number:40515,State:1,Class:15
error:   Reference to database and/or server name in 'samplxxxx.dbo.EVChargers' is not supported in this version of SQL Server.

image

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Apr 25, 2022

@alexdresko I’ve opened #27878 to track that specific request.

0reactions
ajcvickerscommented, Sep 6, 2022

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we’d like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Executing efbundle (post dotnet ef migrations budle) not ...
I am able to run my solution just fine. I am using code first approach for Db migrations using dotnet ef version 7.0.4....
Read more >
Applying Migrations - EF Core
A migration may drop a column when the intent was to rename it, or may fail for various reasons when applied to a...
Read more >
Migrate your database using Entity Framework Core ...
Learn how to use EF Core's new Migration Bundles feature, how to generate bundles, and how to execute them to migrate your databases....
Read more >
Introducing DevOps-friendly EF Core Migration Bundles
EF Core's new migration bundles provide build artifacts to deploy schema and data changes to your database as part of your DevOps pipeline....
Read more >
EF Core 6 new features and changes for .NET 6
Feature #1 - Migration bundles. Entity Framework's migrations have been a familiar feature for code-first integration. As new features get ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found