The normal configuration of the object will be something like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Planet>().HasKey(b => b.Id);
}
However, as you noticed, this will also register the type as part of your model. Entity Framework 6 introduced a method DbModelBuilder.Types<T>that according to the docs:
Begins setting up a lightweight convention that applies to all objects and complex model types that inherit or implement the type specified by a common argument. This method does not register types as part of the model.
This means that you can configure the base entity class as follows:
modelBuilder.Types<Entity>().Configure(c =>
{
c.HasKey(e => e.Id);
});
Which saves you from having to do this for every type that inherits from Entity.
source
share