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

Preview 4: Copy instead of reusing the same reference in AnnotatableBase.AddAnnotations #24781

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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