Skip to content

Non-deterministic user seed data in HasData() (e.g. DateTime.Now) causes constant "model changed" warnings #35080

@Drago95

Description

@Drago95

Repro Steps:

  1. Create initial migration via command line (dotnet ef migrations add "AddDiaryEntryTable" --project .\DiaryApp\DiaryApp.csproj --context ApplicationDbContext)
  2. Apply migration to database (dotnet ef database update --project .\DiaryApp\DiaryApp.csproj)
  3. Uncomment OnModelCreating in ApplicationDbContext
  4. Create a new migration (dotnet ef migrations add "AddedSeedingDataDiaryEntry" --project .\DiaryApp\DiaryApp.csproj --context ApplicationDbContext)
  5. Try to apply the migration to database - get error
dotnet exec --depsfile "C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\bin\Debug\net9.0\DiaryApp.deps.json" --additionalprobingpath C:\Users\dan_fischer.CORP\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPack
ages" --runtimeconfig "C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\bin\Debug\net9.0\DiaryApp.runtimeconfig.json" C:\Users\dan_fischer.CORP\.dotnet\tools\.store\dotnet-ef\9.0.0-rc.2.24474.1\dotnet-ef\9.0.0-rc.2.24474.1\tools\net8.0\any\tools\netcoreapp2.0\any\
ef.dll database update --assembly "C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\bin\Debug\net9.0\DiaryApp.dll" --project "C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\DiaryApp.csproj" --startup-assembly "C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\bin\Debug\net
9.0\DiaryApp.dll" --startup-project "C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\DiaryApp.csproj" --project-dir "C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\\" --root-namespace DiaryApp --language C# --framework net9.0 --nullable --working-dir "C:\Source\repos\Udemy\.NET 9\Diary" --verbose
Using assembly 'DiaryApp'.
Using startup assembly 'DiaryApp'.
Using application base 'C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\bin\Debug\net9.0'.
Using working directory 'C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp'.
Using root namespace 'DiaryApp'.
Using project directory 'C:\Source\repos\Udemy\.NET 9\Diary\DiaryApp\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding DbContext classes in the project...
Found DbContext 'ApplicationDbContext'.
Finding application service provider in assembly 'DiaryApp'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Using context 'ApplicationDbContext'.
Finding design-time services referenced by assembly 'DiaryApp'...
Finding design-time services referenced by assembly 'DiaryApp'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'DiaryApp'...
No design-time services were found.
System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ApplicationDbContext' has pending changes. Add a new migration before updating the database. 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.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ApplicationDbContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

Diary.zip

EF Core version: 9.0.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 9.0)
Operating system: Windows 11 23H2 (Build 22631.4460)
IDE: JetBrains Rider 2024.2.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions