EF6: Object type is not part of the model for the current context

Our system consists of several assemblies. One of them contains the data access layer, which contains the initialization of the DbContext.

Other assemblies (projects) contain some model classes. In our case, the model class is each class decorated with an attribute.

So, in the method, OnModelCreating

I do something like:

modelBuilder.Types()
            .Where(type => type.GetCustomAttributes(false).OfType<TablePrefixAttribute>().Any())
            .Configure(config => config.ToTable(GetTableName(config.ClrType)));

      

I was hoping that the method would Types()

list all the available class types, and so the selector would have at least a chance to assign the correct table name to every class I have 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 you put a breakpoint on the where method, the causing class will never be in that enumeration. For some other types, this works very well.

I know I can do something like:

modelBuilder.Entity<...>().ToTable("...")

      

and with that the error goes away, but that's exactly what I don't want to do. We use strict naming conventions and this allows us to derive table names from class names (just said). So every time I decide to add a table and a class, I don't want to register it anywhere.

I'm confused about this: what types are listed in modelBuilder.Types()

? Why are there some classes and some others?

If I put a breakpoint inside OnModelCreating

, all assemblies containing some model classes are loaded.

What do I do to customize the display?

+3


source to share





All Articles