elsa-workflows / elsa-workflows/elsa-core
Bug: EF Core with MSSQL PendingModelChanges with custom schema in Elsa2 (based on 2.16.1)
- Dominant language
- C#
- Stars
- 7.9k
- Forks
- 1.5k
- Avg merge
- 15h 22m
- Merged PRs (30d)
- 114
Description
## Description
Update: Pull request was created to address this issue: https://github.com/elsa-workflows/elsa-core/pull/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.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)
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
2. **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"});
}
);
```
3. **Attachments**:
- **Sample Project**: https://github.com/fgollas/elsa-bugreport-pendingmodelchanges-2.1.16/tree/main
4. **Reproduction Rate**: 100%
5. **Video/Screenshots**: Not available
6. **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.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
Contributor guide
Research direction
Start with the linked pull request #7578 and the ElsaContext SQL Server setup in the reproduction project. Run the sample with a custom schema, then trace the startup migration path that raises PendingModelChangesWarning. Done means the application starts without the warning when only the schema differs from the migration snapshot.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 20/100