Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core 6.0 temporal tables - extra migrations generated when no change #27911

Closed
slubowsky opened this issue Apr 28, 2022 · 3 comments
Closed

Comments

@slubowsky
Copy link

slubowsky commented Apr 28, 2022

When add temporal table and set history table schema, if add migration again in future, extra incorrect migrations are created.

In DbContext.OnModelCreating made many existing tables temporal by adding .IsTemporal()

modelBuilder.Entity<MyEntity>()
               .ToTable("MyEntity", b => b.IsTemporal());

Set history schema and temporal column names on all like this

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
    if (entityType.IsTemporal())
    {
       entityType.SetHistoryTableSchema("history");
       entityType.SetPeriodStartPropertyName("ValidFrom");
       entityType.SetPeriodEndPropertyName("ValidTo");
    }
}

Generated and applied migrations to DB. All seems well. Make additional changes and generate new migrations. Get an incorrect migration for each temporal table in addition to the expected migrations for the real changes:

migrationBuilder.AlterTable(
      name: "MyEntity")
      .Annotation("SqlServer:IsTemporal", true)
      .Annotation("SqlServer:TemporalHistoryTableName", "MyEntityHistory")
      .Annotation("SqlServer:TemporalHistoryTableSchema", "history")
      .Annotation("SqlServer:TemporalPeriodEndColumnName", "ValidTo")
      .Annotation("SqlServer:TemporalPeriodStartColumnName", "ValidFrom")
      .OldAnnotation("SqlServer:IsTemporal", true)
      .OldAnnotation("SqlServer:TemporalHistoryTableName", "MyEntityHistory")
      .OldAnnotation("SqlServer:TemporalHistoryTableSchema", null)
      .OldAnnotation("SqlServer:TemporalPeriodEndColumnName", "ValidTo")
      .OldAnnotation("SqlServer:TemporalPeriodStartColumnName", "ValidFrom");

Edit - deleted all existing migrations and just generated a first one immediately followed by another. Same incorrect behavior. Incorrect migrations included in second for all temporal tables despite no changes having been made.

EF Core version: 6.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.1.6

@maumar
Copy link
Contributor

maumar commented May 10, 2022

dupe of #26676

@slubowsky as a workaround on 6.0.4 you can use:

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<MyEntity>().ToTable(b => b.IsTemporal(ttb => ttb.UseHistoryTable("history", "MyEntityHistory")));

                foreach (var entityType in modelBuilder.Model.GetEntityTypes())
                {
                    if (entityType.IsTemporal())
                    {
                        entityType.SetPeriodStartPropertyName("ValidFrom");
                        entityType.SetPeriodEndPropertyName("ValidTo");
                    }
                }
        }

@slubowsky
Copy link
Author

@maumar ok thanks. I see its already marked as fixed for 7.0 (and maybe a 6.x release?). Until it hits, I think Ill just continue to manually remove the unwanted migrations rather then add that extra code to the many tables since it soon wont be needed.

@maumar
Copy link
Contributor

maumar commented May 11, 2022

@slubowsky it's fixed in current 7.0 preview but we rejected it for 6.x patch.

@maumar maumar closed this as completed May 11, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants