Skip to content

Commit

Permalink
Make SnapshotModelProcessor idempotent.
Browse files Browse the repository at this point in the history
Fixes #35146
  • Loading branch information
AndriySvyryd committed Jan 8, 2025
1 parent c434d6c commit 20c9b27
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public SnapshotModelProcessor(
/// </summary>
public virtual IModel? Process(IReadOnlyModel? model, bool resetVersion = false)
{
if (model == null)
if (model == null
|| model is not Model mutableModel
|| mutableModel.IsReadOnly)
{
return null;
}
Expand All @@ -79,13 +81,10 @@ public SnapshotModelProcessor(
}
}

if (model is IMutableModel mutableModel)
mutableModel.RemoveAnnotation("ChangeDetector.SkipDetectChanges");
if (resetVersion)
{
mutableModel.RemoveAnnotation("ChangeDetector.SkipDetectChanges");
if (resetVersion)
{
mutableModel.SetProductVersion(ProductInfo.GetVersion());
}
mutableModel.SetProductVersion(ProductInfo.GetVersion());
}

return _modelRuntimeInitializer.Initialize((IModel)model, designTime: true, validationLogger: null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void Updates_provider_annotations_on_model()

var reporter = new TestOperationReporter();

new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance).Process(model);
var processor = new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance);
processor.Process(model);

AssertAnnotations(model);
AssertAnnotations(entityType);
Expand All @@ -54,6 +55,8 @@ public void Updates_provider_annotations_on_model()
AssertAnnotations(nav2);
AssertAnnotations(index);

Assert.Same(model, processor.Process(model));

Assert.Empty(reporter.Messages);
}

Expand Down

0 comments on commit 20c9b27

Please sign in to comment.