2

Currently I am using separate configuration files and calling them like:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new ProductConfiguration());
    base.OnModelCreating(modelBuilder);
}

It seems like most of the examples online are really basic so they define their models, the DbContext and the model configurations all in a single class. Are there and performance issues or other compelling reasons to use one over the other?

jpshook
  • 4,834
  • 6
  • 36
  • 45
  • I see nothing wrong at all with what you're doing. – Chris Marisic May 16 '11 at 21:14
  • @Chris Marisic - Thanks for the vote of confidence! All 3 variations seem to have slightly different syntax and it is hard to find a solid set of examples that cover most scenarios and use external entity config classes. – jpshook May 17 '11 at 13:09

2 Answers2

4

I don't know what you mean exactly with "configuration files" but there are indeed basically three options to define a model:

  • Conventions: When you create your model classes you name your properties in a way that EF can detect primary keys, foreign keys, relationships and so on automatically. If you do this consequently you neither need any data annotations nor to overwrite OnModelCreating.

  • Data annotations: Useful when you cannot or don't want to follow the convention rules. An example might be that you have an existing database whose column names doesn't match the standard naming rules of EF, for instance if you have a key column with a name which EF wouldn't recognize as a key:

    public class User
    {
        [Key]
        public int User_Code { get; set; }
    }
    
  • Fluent API in OnModelCreating: For advanced mapping scenarios which you can't define with data annotations. Examples are here.

For the performance I believe it doesn't matter what you use. The approach is a question of taste and of the complexity of your model. EF creates an internal model represention only once during the lifetime of an application instance. (You can see this by setting a breakpoint in OnModelCreating: You'll reach this breakpoint only once, no matter how often you create a new DbContext.)

Community
  • 1
  • 1
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • 1
    I believe "configuration files" are classes derived from `EntityConfiguration` registered in `OnModelCreating` – Ladislav Mrnka May 17 '11 at 08:42
  • I provided example code.. modelBuilder.Configurations.Add(new ProductConfiguration()); Basically, they are external classes where you specify the mapping and rules for the model using the fluent API. – jpshook May 17 '11 at 13:02
  • So, would it be advisable to use data annotations for models that need only limited configurations OR better to just use one method and stick with it for all models? – jpshook May 17 '11 at 13:06
  • @Developr: Now I see, actually I'm doing the same. I was confused by the term "files" and thought there is some XML configuration or something I've never seen. Didn't read well enough, sorry. To your question: If you can define your mapping with both data annotations and Fluent API it is really a matter of taste in my opinion. There is no performance difference. Some people don't like to have annotations in their POCO classes and always prefer therefore the Fluent code. On the other hand annotations are easy to apply, you don't need to write code and don't need a separate configuration class. – Slauma May 17 '11 at 14:29
  • In the beginning I have used Fluent API mostly, no matter if it was necessary or not. In newer models I'm actually starting to appreciate the EF designer's intention: 1) Define your model properties and names so that EF does the correct mapping by convention (you don't need neither annotations nor Fluent code), 2) If the conventions don't meet your requirements, apply appropriate annotations, 3) If there are no appropriate annotations, use Fluent API. But as said, it's a matter of taste to follow this approach. – Slauma May 17 '11 at 14:31
1

No, it's only a matter of readability. As your model grows you will probably get a very large configuration in OnModelCreating. As to make this more readable you break it up into separate configurations.

Per
  • 1,393
  • 16
  • 28
  • +1 this is a very important justification, it also starts to fall into the grey area of SRP violations whereas using the configuration files keep that separate. – Chris Marisic May 17 '11 at 13:36