Our system consists of several assemblies. One of them contains the data access layer that contains a DbContext initialization.
The other assemblies (projects) contains some model classes. In our case a model class is every class decorated by an attribute.
So in the OnModelCreating method I do something like:
modelBuilder.Types()
.Where(type => type.GetCustomAttributes(false).OfType<TablePrefixAttribute>().Any())
.Configure(config => config.ToTable(GetTableName(config.ClrType)));
I hoped the Types() method enumerates through all available class types and therefore the where selector will have at least a chance to assign the correct table name to every class I need to do it for.
However, for some types (at least one) I get this error:
The entity type ... is not part of the model for the current context
If I put a breakpoint to the where method the class causing the error is never in this enumeration. For some other types this works pretty well.
I know I can do something like:
modelBuilder.Entity<...>().ToTable("...")
and with this the error disappears, but this is exactly the thing I do not want to do. We are using strict naming conventions and this allows us to obtain table names from class names (simply said). So everytime I decide to add a table and class I do not want to have to register it anywhere.
I am confused about this: what types are listed in modelBuilder.Types()? Why there are some classes present while some other not?
If I put a breakpoint inside the OnModelCreating, all assemblies that contain some model classes are loaded.
What have I do to get my mapping working?