efcore icon indicating copy to clipboard operation
efcore copied to clipboard

`dotnet ef migrations` does not work correctly with more than one model and migrations added in a non default location.

Open EmperorArthur opened this issue 1 year ago • 2 comments

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

  1. Use the ApplicationDbContext below.
  2. Ensure that both the following folders do not exist. "Migrations", "Data/Migrations", and that no migrations have been created.
  3. Add an additional DbContext with a different name.
  4. Run the command dotnet ef migrations add -o "Data/Migrations" --context ApplicationDbContext CreateDataProtectionKeys
  5. Replace "DbContext" in the code below with "IdentityDbContext"
  6. 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 ApplicationDbContext works.

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 ApplicationDbContext fails 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.

EmperorArthur avatar Aug 26 '24 04:08 EmperorArthur

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.

neominky avatar Sep 14 '24 16:09 neominky

Duplicate of #32738

ajcvickers avatar Dec 01 '24 14:12 ajcvickers