EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

Inheritance page not comprehensive: lacks code snippets and needs more details.

Open amal-stack opened this issue 5 years ago • 1 comments

This page only includes how EF Core would configure TPH by default when conventions are relied upon. Information regarding its configuration and customization using the Fluent API is skipped.

For example:

If you don't want to expose a DbSet for one or more entities in the hierarchy, you can also use the Fluent API to ensure they are included in the model.

In the above statement, use the Fluent API is like telling the reader that it can be done but go and find out how its done. It would be quite useful if ways to explicitly map derived types when not declared as properties in the DbContext is demonstrated. Also, it would be great if it is specified that this explicit mapping is not required when child types are assigned a discriminator value using HasDiscriminator<type>("disc_name").HasValue<ChildType>(value) .

To take advantage of the polymorphic capabilities of inheritance, it is a very common practice to store references to more derived types in the base class reference. Hence, it is worth mentioning with code snippets how to have a single collection DbSet<Base> referencing specific types and then using OfType() to query for more specific instances and if casting is legal.

The page also does not mention how to configure TPH when the base class is abstract. Neither does it mention if the base class is allowed to be declared abstract in the first place.

Another tip on the page that is a bit vague is:

If you don't rely on conventions, you can specify the base type explicitly using HasBaseType. You can also use .HasBaseType((Type)null) to remove an entity type from the hierarchy.

A code snippet here would help a lot,

I would argue that Inheritance in EF Core requires its own section in the documentation and TPT should be moved to its separate page. Considering that this is the only page dedicated to Inheritance in EF Core, it needs to be comprehensive enough to include common use cases.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

amal-stack avatar Jan 29 '21 09:01 amal-stack

And now 18 months have passed ....

skrabbe001 avatar Aug 12 '22 01:08 skrabbe001