`dotnet ef migrations` does not work correctly with more than one model and migrations added in a non default location.
Issue
ApplicationDbContextModelSnapshot is partially ignored if it is not in the top level "Migrations" folder, and there is more than one DbContext. I am unsure if the 2nd DbContext is needed to trigger this bug.
- When creating subsequent migrations, they act as though they are the first migration, and ignore all prior migrations.
dotnet ef migrations remove --context ...gives an error of "No ModelSnapshot was found."
Steps To Reproduce
- Use the ApplicationDbContext below.
- Ensure that both the following folders do not exist. "Migrations", "Data/Migrations", and that no migrations have been created.
- Add an additional DbContext with a different name.
- Run the command
dotnet ef migrations add -o "Data/Migrations" --context ApplicationDbContext CreateDataProtectionKeys - Replace "DbContext" in the code below with "IdentityDbContext"
- Run the command
dotnet ef migrations add -o "Data/Migrations" --context ApplicationDbContext CreateIdentitySchema.
public class ApplicationDbContext : DbContext, IDataProtectionKeyContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<DataProtectionKey> DataProtectionKeys { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Model.SetDefaultSchema("SomeSchema");
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<DataProtectionKey>(entityTypeBuilder =>
{
entityTypeBuilder.Property(e => e.Xml).HasMaxLength(-1);
});
}
}
Expected Results
- All migrations, and "ApplicationDbContextModelSnapshot.cs" are created in "Data/Migrations".
- CreateIdentitySchema migration works, and does not attempt to re-create the DataProtectionKeys table.
dotnet ef migrations remove --context ApplicationDbContextworks.
Actual Results
- All migrations, and "ApplicationDbContextModelSnapshot.cs" are created in "Data/Migrations".
- CreateIdentitySchema migration attempts to re-create the DataProtectionKeys table, so fails.
dotnet ef migrations remove --context ApplicationDbContextfails with "No ModelSnapshot was found".
Provider and Version information
- EF Core version: 8.0.8
- Database provider: Microsoft.EntityFrameworkCore.SqlServer)
- Target framework: NET 8.0
- Operating system: Windows 10 Enterprise LTSC 10.0.17763 Build 17773
Other Information
This does not occur if the very first migration does not use the "-o" switch. In that case, "Models/ApplicationDbContextModelSnapshot.cs" continues to be used, and migrations are correct and can be removed.
Which is what makes this issue so frustrating is Visual Studio by default creates new projects using the "Data/Migrations" convention. However, trying to start over runs into this bug work.
I came here because of the same error "No ModelSnapshot was found.". And I solved the problem. I made it not compile to renew the migration folder and moved the files. add-migration worked fine and a new snapshot file was created, but the project was not compiling the snapshot because of the previous settings. Check in the properties window(Visual Studio) to see if the snapshot code file is being compiled.
Duplicate of #32738