Skip to content

Commit 4623a39

Browse files
authored
Make SnapshotModelProcessor idempotent. (#35543)
Fixes #35146
1 parent 59c01a5 commit 4623a39

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public SnapshotModelProcessor(
5252
/// </summary>
5353
public virtual IModel? Process(IReadOnlyModel? model, bool resetVersion = false)
5454
{
55-
if (model == null)
55+
if (model == null
56+
|| model is not Model mutableModel
57+
|| mutableModel.IsReadOnly)
5658
{
5759
return null;
5860
}
@@ -79,13 +81,10 @@ public SnapshotModelProcessor(
7981
}
8082
}
8183

82-
if (model is IMutableModel mutableModel)
84+
mutableModel.RemoveAnnotation("ChangeDetector.SkipDetectChanges");
85+
if (resetVersion)
8386
{
84-
mutableModel.RemoveAnnotation("ChangeDetector.SkipDetectChanges");
85-
if (resetVersion)
86-
{
87-
mutableModel.SetProductVersion(ProductInfo.GetVersion());
88-
}
87+
mutableModel.SetProductVersion(ProductInfo.GetVersion());
8988
}
9089

9190
return _modelRuntimeInitializer.Initialize((IModel)model, designTime: true, validationLogger: null);

test/EFCore.Design.Tests/Migrations/Design/SnapshotModelProcessorTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public void Updates_provider_annotations_on_model()
4343

4444
var reporter = new TestOperationReporter();
4545

46-
new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance).Process(model);
46+
var processor = new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance);
47+
processor.Process(model);
4748

4849
AssertAnnotations(model);
4950
AssertAnnotations(entityType);
@@ -54,6 +55,8 @@ public void Updates_provider_annotations_on_model()
5455
AssertAnnotations(nav2);
5556
AssertAnnotations(index);
5657

58+
Assert.Same(model, processor.Process(model));
59+
5760
Assert.Empty(reporter.Messages);
5861
}
5962

0 commit comments

Comments
 (0)