Skip to content

Commit

Permalink
Copy instead of reusing the same reference in AnnotatableBase.AddAnno…
Browse files Browse the repository at this point in the history
…tations (#24781)

Fixes #24658

Co-authored-by: Andriy Svyryd <[email protected]>
  • Loading branch information
ajcvickers and AndriySvyryd authored Apr 27, 2021
1 parent f212a16 commit 5cb55a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/EFCore/Infrastructure/AnnotatableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal static void AddAnnotations(AnnotatableBase annotatable, IEnumerable<IAn
{
foreach (var annotation in annotations)
{
annotatable.AddAnnotation(annotation.Name, (Annotation)annotation);
annotatable.AddAnnotation(annotation.Name, annotation.Value);
}
}

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

private ConcurrentDictionary<string, Annotation> GetOrCreateRuntimeAnnotations()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3708,7 +3708,7 @@ void ValidateFixup(DbContext context, IList<EntityOne> leftEntities, IList<Entit
Assert.Equal(3, context.ChangeTracker.Entries<EntityOne>().Count());
Assert.Equal(3, context.ChangeTracker.Entries<EntityTwo>().Count());
Assert.Equal(5, context.ChangeTracker.Entries<JoinOneToTwo>().Count());
Assert.Equal(1, context.ChangeTracker.Entries<JoinOneToTwoExtra>().Count());
Assert.Single(context.ChangeTracker.Entries<JoinOneToTwoExtra>());

Assert.Equal(3, leftEntities[0].TwoSkip.Count);
Assert.Single(leftEntities[1].TwoSkip);
Expand Down
16 changes: 16 additions & 0 deletions test/EFCore.Tests/Infrastructure/AnnotatableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public void Can_add_and_remove_annotation()
Assert.Null(annotatable.FindAnnotation("Foo"));
}

[ConditionalFact]
public void Added_annotation_type_is_consistent()
{
var annotatable = new Annotatable();

annotatable.AddAnnotations(new[] { new ConventionAnnotation("Foo", "Bar", ConfigurationSource.Convention) });

Assert.Equal(typeof(Annotation), annotatable.FindAnnotation("Foo").GetType());

var conventionAnnotatable = new Model();

conventionAnnotatable.AddAnnotations(new[] { new Annotation("Foo", "Bar") });

Assert.Equal(typeof(ConventionAnnotation), conventionAnnotatable.FindAnnotation("Foo").GetType());
}

[ConditionalFact]
public void Adding_duplicate_annotation_throws()
{
Expand Down

0 comments on commit 5cb55a8

Please sign in to comment.