dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

Document that calling HasBaseType((Type)null) is usually wrong #2597

Open jasekiw opened 4 years ago

jasekiw commented 4 years ago

In the documentation there is a Tip section that says the following.

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.

I am specifically interested in the following sentence:

You can also use .HasBaseType((Type)null) to remove an entity type from the hierarchy

I think it would be best for there to be more than just one sentence on this topic. There are no examples of the use cases this provides.

My specific use case

A specific use case that I am trying to implement is to have 3 entities. 1 base class, and two subclasses. The base class has some default columns that both subclasses need but I don't want the base class has no real use in the database. Right now I am working around this issue by creating a placeholder value called Null in the discriminator enum.

public enum EEntityType
{
    Null, // I want to get rid of this since the base class is not needed.
    SubClass1,
    SubClass2,
}

Does .HasBaseType((Type)null) fill this need? If so I think an example needs to be added to understand the use cases of this method better.


Document Details

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

ajcvickers commented 4 years ago

@jasekiw Thanks for filing this. In reality, it's almost always wrong to call this. Most commonly in cases like this the base type should not be mapped at all. So the most common correct resolution is to find out why it is being mapped (e.g. DbSet on the context, relationships to the type, etc.) and then correct that. If you post your model classes and OnModelCreating code, then we can take a closer look.