I have a BaseClass, which is abstract, and has many abstract properties.
I have a dozen or so (it will probably grow) entities that are part of the Entity Framework that each derive from BaseClass.
I'm trying to avoid having to do:
modelBuilder.Entity<Entity1>().HasKey(t => t.Id);
modelBuilder.Entity<Entity2>().HasKey(t => t.Id);
modelBuilder.Entity<Entity3>().HasKey(t => t.Id);
...
for each property and each entity, since that seems very wasteful and creates a lot of code duplication. I experimented with getting all the Entities in a namespace that derive from the BaseClass by:
var derivedEntities = Assembly.GetExecutingAssembly().GetTypes().
Where(t => t.Namespace == "My.Entities" && t.IsAssignableFrom(typeof(BaseClass)));
However, the next logical steps seems to be:
foreach (var entity in derivedEntities)
{
modelBuilder.Entity<entity>().HasKey(t => t.Id);
}
but will not compile, because
"entity is a variable, but is used like a type".