You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an application with a database project and a data migration project. I wrote a unit test in a dedicated project to test there is no pending changes in my model and no migration is required.
publicclassAppDbContextTests{[Test]publicvoidDatabase_shouldNotHavePendingModelChanges(){using(varcontext= CreateAppDbContext()){varhasChanges= context.Database.HasPendingModelChanges();
Assert.That(hasChanges, Is.False,"The dabatase has model changes pending. Create a new migration for it.");}}private AppDbContext CreateAppDbContext(){varconnection=new SqliteConnection("Filename=:memory:");
connection.Open();vardbContextOptions=newDbContextOptionsBuilder<AppDbContext>().UseSqlite(
connection,b => b.MigrationsAssembly(Assembly.GetAssembly(typeof(DesignTimeDbContextFactory))!.GetName().Name)).Options;returnnew AppDbContext(dbContextOptions);}}
This test always fails. HasPendingModelChanges always returns true
This is the code of DesignTimeDbContextFactory.cs
publicclassDesignTimeDbContextFactory:IDesignTimeDbContextFactory<AppDbContext>{public AppDbContext CreateDbContext(string[]args){// Build configIConfigurationconfig=new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
# if DEBUG
.AddJsonFile($"appsettings.local.json")
#endif
.AddEnvironmentVariables().Build();// Get connection stringvaroptionsBuilder=newDbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseSqlServer(config.GetValue<string>(AppDbContext.ConfigurationKey),b => b.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name));returnnew AppDbContext(optionsBuilder.Options);}}
When I open a terminal from the Database.Migrations folder and run the command dotnet ef migrations has-pending-model-changes it gives me the correct result.
I tried using the same database provider in my unit test but the result was the same. It doesn't look to be related to that.
EF Core version: 8.0.10
Target framework: (e.g. .NET 8.0)
Operating system: Windows 11
IDE: (e.g. Visual Studio 2022 17.11.5)
The text was updated successfully, but these errors were encountered:
@charlesRollandy I may be missing something here, but if you create a new, empty, SQLite in-memory database, then the schema will be empty and HasPendingModelChanges should always return true.
@ajcvickersHasPendingModelChanges is not looking if the model changes are applied to the database but if some changes into the entities are not reflected with migrations.
What is actually weird is that the cli provides the correct result but not the method from the SDK. Could if be related to configuration ?
I have an application with a database project and a data migration project. I wrote a unit test in a dedicated project to test there is no pending changes in my model and no migration is required.
My solution has the following structure:
This us my unit test in AppDbContext.cs:
This test always fails.
HasPendingModelChanges
always returns trueThis is the code of DesignTimeDbContextFactory.cs
When I open a terminal from the
Database.Migrations
folder and run the commanddotnet ef migrations has-pending-model-changes
it gives me the correct result.I tried using the same database provider in my unit test but the result was the same. It doesn't look to be related to that.
EF Core version: 8.0.10
Target framework: (e.g. .NET 8.0)
Operating system: Windows 11
IDE: (e.g. Visual Studio 2022 17.11.5)
The text was updated successfully, but these errors were encountered: