Skip to content

Bug: EF Core with MSSQL PendingModelChanges with custom schema in Elsa2 (based on 2.16.1) #7577

Description

@fgollas

Description

Update: Pull request was created to address this issue: #7578

When using Elsa2 EF Core persistence with SQL Server and a custom schema (for example Elsa-Test), startup fails with an EF Core pending model changes error even though no functional model changes were made.

The issue appears to be caused by schema-name differences between the runtime model and migration snapshot (schema-only drift), which EF treats as a model difference.

Error: System.InvalidOperationException
An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ElsaContext' has pending changes. Add a new migration before updating the database. See https://aka.ms/efcore-docs-pending-changes. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition1.Log[TLoggerCategory](IDiagnosticsLogger1 logger, TParam arg)
at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.ValidateMigrations(Boolean useTransaction)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
at Elsa.Persistence.EntityFramework.Core.StartupTasks.RunMigrations.ExecuteAsync(CancellationToken

Steps to Reproduce

To help us identify the issue more quickly, please follow these guidelines:

  1. Detailed Steps:
    a) Create a project using Elsa2 EF Core SQL Server persistence (baseline: tag 2.16.1).
    b) Configure ElsaContext to use a custom schema (for example Elsa-Test) instead of the default Elsa.
    c) Ensure migrations/snapshot are based on default schema mappings (existing Elsa snapshot behavior).
    d) Start the application (or run migration validation/startup path that checks model drift).
    e) Observe PendingModelChanges exception.

Here is a minimal sample to reproduce the issue: https://github.com/fgollas/elsa-bugreport-pendingmodelchanges-2.1.16/tree/main

  1. Code Snippets:
elsa.UseEntityFrameworkPersistence(opts => {
  Elsa.Persistence.EntityFramework.SqlServer.DbContextOptionsBuilderExtensions.UseSqlServer(
      opts,
      "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=elsa-test;Integrated Security=True;Connect Timeout=30;Encrypt=False;",
        typeof(SqlServerElsaContextFactory)
      , new ElsaDbContextOptions() {SchemaName = "Custom-Schema"});
  }
);
  1. Attachments:

  2. Reproduction Rate: 100%

  3. Video/Screenshots: Not available

  4. Additional Configuration: In the provided sample you need to specify a valid connectionstring to some existing mssql database.

Expected Behavior

Using a custom schema with Elsa2 EF Core persistence should not trigger PendingModelChanges when the only difference is schema naming and the model is otherwise equivalent. The application should start successfully without requiring a new migration for schema-only drift.

Actual Behavior

Currently EFCore (9) throws an exception

Screenshots

N/A

Environment

  • Elsa Package Version: 2.16.1
  • Operating System: Microsoft Windows 11 Pro, Version 10.0.22631 Build 22631
  • Browser and Version: N/A

Log Output

fail: Microsoft.Extensions.Hosting.Internal.Host[11]
      Hosting failed to start
      System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ElsaContext' has pending changes. Add a new migration before updating the database. See https://aka.ms/efcore-docs-pending-changes. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
         at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`1.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, TParam arg)
         at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType)
         at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.ValidateMigrations(Boolean useTransaction, String targetMigration)
         at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
         at Elsa.Persistence.EntityFramework.Core.StartupTasks.RunMigrations.ExecuteAsync(CancellationToken cancellationToken)
         at Elsa.Persistence.EntityFramework.Core.StartupTasks.RunMigrations.ExecuteAsync(CancellationToken cancellationToken)
         at Elsa.Runtime.StartupRunner.StartupAsync(CancellationToken cancellationToken)
         at Elsa.Runtime.StartupRunner.StartupAsync(CancellationToken cancellationToken)
         at Elsa.HostedServices.StartupRunnerHostedService.StartAsync(CancellationToken cancellationToken)
         at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__14_1(IHostedService service, CancellationToken token)
         at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)

Troubleshooting Attempts

There's a workarount disabling the Relational Event via EFCore's dbcontextoptions.
Have implemented a potential solution and will push a PR linked to this bugreport

Additional Context

N/A

Related Issues

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions