Design Meeting Notes - November 20, 2012

Automatic EntityTypeConfiguration discovery

Code First configuration for entity types can be done directly in OnModelCreating or in classes that inherit from EntityTypeConfiguration. However, as described by a potential contributor, each EntityTypeConfiguration still needs to be registered on the model builder, usually in OnModelCreating. This can increase coupling and result in a lot of boilerplate code. The proposal from the potential contributor is to allow types that inherit from EntityTypeConfiguration to be discovered in an assembly and automatically added to the model builder/registrar.

We would be happy to accept this contribution, although with a few modifications from the original proposal. The motivation for the modifications is to keep the feature simple and easy to understand with a low concept count and with minimal and clean changes to the public API surface. The modifications are:

  • We don’t believe that there needs to be a new type derived from EntityTypeConfiguration. When AddFromAssembly is called it should discover any type that inherits from EntityTypeConfiguration and automatically add it to the model builder. Adding a new type would allow for filtering, but the general consensus was that this feature is really about making it easy to add all configurations in an assembly. If filtering is needed, then it is easy to write such filtering externally to core EF and use whatever mechanism the developer chooses to do the filtering—for example, an attribute might be preferred over a new type.
  • We believe that there should be only one new method on ConfigurationRegistrar:
    void AddFromAssembly(Assembly assembly)
    • This method takes an Assembly instance. It is quite easy to get that assembly instance in whatever way the caller wants to do it without adding overloads to the API.
    • If we get a lot of requests for other overloads (or it becomes apparent that they are very useful) then we would consider adding them, but we will start with just one method.

Last edited Dec 14, 2012 at 6:03 PM by ajcvickers, version 2

Comments

Moerie Feb 5, 2013 at 8:35 AM 
What about using Dependency Injection to set an IEnumerable<EntityTypeConfiguration>? Will code migrations still work then?