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

IAnnotatable.FindAnnotation does not find annotations recursively #20374

Closed
obohaciak opened this issue Mar 23, 2020 · 5 comments
Closed

IAnnotatable.FindAnnotation does not find annotations recursively #20374

obohaciak opened this issue Mar 23, 2020 · 5 comments

Comments

@obohaciak
Copy link

obohaciak commented Mar 23, 2020

When calling FindAnnotation on a derived entity type, the annotations declared on the base type are not returned.

Steps to reproduce

Consider following model:

EntityType: BaseType Abstract
  ... (cut for brevity)
  Annotations: 
    MyAnnotation: System.String

EntityType: DerivedType Base: BaseType

Searching for the annotation on derived type:

IEntityType derivedEntityType = model.FindEntityType(typeof(DerivedType).FullName);
// GetAnnotations below returns Count = 0
IEnumerable<IAnnotation> annotations = derivedEntityType.GetAnnotations();
// FindAnnotation below returns null
IAnnotation annotation = derivedEntityType.FindAnnotation("MyAnnotation");

Whereas, searching for the annotation on base type:

IEntityType baseEntityType = model.FindEntityType(typeof(BaseType).FullName);
// GetAnnotations below returns Count = 1
IEnumerable<IAnnotation> annotations  = baseEntityType.GetAnnotations();
// FindAnnotation below returns MyAnnotation
IAnnotation annotation = baseEntityType.FindAnnotation("MyAnnotation");

If we compare it analogous FindProperty, and FindDeclaredProperty, perhaps this FindAnnotation should have been FindDeclaredAnnotation instead to highlight the fact it searches the given type only whereas "FindAnnotation" implies that it works recursively.

It would be a (limited?) breaking change though, so perhaps something for a major release like .NET 5?

Further technical details

EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10.0.18363.720
IDE: Visual Studio 2019 Enterprise 16.5.0

@obohaciak
Copy link
Author

A little correction, GetAnnotations also doesn't work recursively.

@ajcvickers
Copy link
Member

@obohaciak This is currently by-design. Can you explain a bit more about why you need them to be recursive?

@AndriySvyryd
Copy link
Member

The difference between properties, navigations and annotations is that there can only be 1 property or navigation with the same name in a hierarchy, but there can be more than 1 annotation with the same name.

@obohaciak
Copy link
Author

@ajcvickers Of course, I'll try to explain. I was inspired by an example given by @rowanmiller in #7586 in which he shows how to use custom annotations to add support for read-only entities. My case is similar - track entities strongly related to each other, much like a composition relationship. E.g. an entity EnginePart exists within and cannot exist without an Engine. However Engine is an abstract type, a base type of larger type hierarchy with derived types like CarEngine or PlaneEngine. The custom annotation may exist on any level in the hierarchy. I therefore need to walk the inheritance chain and ask each entity type if it defines the annotation. I hope that that makes sense.

I understand though that as @AndriySvyryd noted, an annotation can be defined multiple times in the type hierarchy and the result of FindAnnotation could therefore be ambiguous, dependent on the type at runtime.

@ajcvickers
Copy link
Member

@obohaciak Thanks for additional info. Closing this as by-design for now.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants