Skip to content

Commit 37ac8ec

Browse files
authored
Copy instead of reusing the same reference in AnnotatableBase.AddAnnotations
Fixes #24658
1 parent d078573 commit 37ac8ec

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Diff for: src/EFCore/Infrastructure/AnnotatableBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ internal static void AddAnnotations(AnnotatableBase annotatable, IEnumerable<IAn
105105
{
106106
foreach (var annotation in annotations)
107107
{
108-
annotatable.AddAnnotation(annotation.Name, (Annotation)annotation);
108+
annotatable.AddAnnotation(annotation.Name, annotation.Value);
109109
}
110110
}
111111

@@ -429,7 +429,7 @@ public virtual TValue GetOrAddRuntimeAnnotationValue<TValue, TArg>(
429429
/// <param name="value"> The value to be stored in the annotation. </param>
430430
/// <returns> The newly created annotation. </returns>
431431
protected virtual Annotation CreateRuntimeAnnotation(string name, object? value)
432-
=> new Annotation(name, value);
432+
=> new (name, value);
433433

434434
private ConcurrentDictionary<string, Annotation> GetOrCreateRuntimeAnnotations()
435435
{

Diff for: test/EFCore.Specification.Tests/ManyToManyTrackingTestBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3708,7 +3708,7 @@ void ValidateFixup(DbContext context, IList<EntityOne> leftEntities, IList<Entit
37083708
Assert.Equal(3, context.ChangeTracker.Entries<EntityOne>().Count());
37093709
Assert.Equal(3, context.ChangeTracker.Entries<EntityTwo>().Count());
37103710
Assert.Equal(5, context.ChangeTracker.Entries<JoinOneToTwo>().Count());
3711-
Assert.Equal(1, context.ChangeTracker.Entries<JoinOneToTwoExtra>().Count());
3711+
Assert.Single(context.ChangeTracker.Entries<JoinOneToTwoExtra>());
37123712

37133713
Assert.Equal(3, leftEntities[0].TwoSkip.Count);
37143714
Assert.Single(leftEntities[1].TwoSkip);

Diff for: test/EFCore.Tests/Infrastructure/AnnotatableTest.cs

+16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ public void Can_add_and_remove_annotation()
3636
Assert.Null(annotatable.FindAnnotation("Foo"));
3737
}
3838

39+
[ConditionalFact]
40+
public void Added_annotation_type_is_consistent()
41+
{
42+
var annotatable = new Annotatable();
43+
44+
annotatable.AddAnnotations(new[] { new ConventionAnnotation("Foo", "Bar", ConfigurationSource.Convention) });
45+
46+
Assert.Equal(typeof(Annotation), annotatable.FindAnnotation("Foo").GetType());
47+
48+
var conventionAnnotatable = new Model();
49+
50+
conventionAnnotatable.AddAnnotations(new[] { new Annotation("Foo", "Bar") });
51+
52+
Assert.Equal(typeof(ConventionAnnotation), conventionAnnotatable.FindAnnotation("Foo").GetType());
53+
}
54+
3955
[ConditionalFact]
4056
public void Adding_duplicate_annotation_throws()
4157
{

0 commit comments

Comments
 (0)