Entity Framework Pitfalls: DbConfiguration Classes Are Automatically Loaded

Entity Framework, since version 6, allows us to specify certain aspects of its configuration by code through a DbConfiguration-derived class. This configuration can be specified in one of three ways:


However, if there is a DbConfiguration-derived class in the same assembly as your DbContext, it will be automatically set as the configuration class, provided that:

  • It is public;
  • It is non-abstract;
  • It has a public parameterless constructor.


So, a DbConfiguration in the same assembly as the DbContext is automatically loaded. If there is more than one, the DbContext constructor will throw an exception.

                             

2 Comments

  • "DbConfiguration Classes Are Automatically Loaded"
    Is this a bad thing or pitfall? I don't see any problem with that.

  • Hi, Saeid!
    By pitfall here I mean something that we might not expect, not necessarily bad. It could be a problem if you are to have multiple DbConfiguration classes and you would like to choose one or the other.

Add a Comment

As it will appear on the website

Not displayed

Your website