There are two ways to configure validation rules and schema definition rules for EF core models. First one, is using attributes, which we have already seen in previous article. The other one is using Fluent API. In this article, let’s see how fluent API can be used for configuring models.
How fluent API works ?
EF Core models can be used to setup interaction of .NET applications using Entity Framework Core. A context should be derived from DbContext. The derived context can override OnModelCreating method provided by DbContext to configure EF core models.
So, the Fluent API configurations are outside the models classes, hence they can be applied without modifying the model classes.
How to apply Fluent API Rules ?
Below code example shows how Fluent API can be used.
UniversityContext class overrides OnModelCreating method and specifies the configuration rules.
Organize Fluent API Rules
We had only one model, Student and even then the method has around 10-15 lines of code. Real world database might need a lot of models and hence this method might grow very quickly. If this method is very big, it might be good from maintainability perspective.
The size of OnModelCreating method can be reduced by extracting configuration rules for an entity into an implementation derived from IEntityTypeConfiguration<TEntity>. So, for every model which corresponds to a table, there will be one implementation derived from IEntityTypeConfiguration<TEntity>.
Below example shows how IEntityTypeConfiguration<TEntity> can be implemented and called from
There are 3 different ways to call this new implementation from DbContext. As shown in below code snippet:
- First option is to call Configure method on the studentConfigurations object
- Second option is to call ApplyConfigurations on modelBuilder instance
- Third option is to call ApplyConfigurationsFromAssembly method to load all IEntityTypeConfiguration<TEntity> implementations from a given assembly. I personally would prefer this way, as it allows to add new configurations without changing DbContext implementation.
Attributes vs Fluent API ?
We have seen two different ways to apply configurations to EF core models – either via attributes or via Fluent APIs. It might be confusing to understand which approach should be used in real world projects. There is no single and definite answer to this one.
- If you do not like to mix validation rules with entity definitions, you may prefer fluent APIs over attributes.
- If your application already using attributes for validation data objects for APIs, you may prefer attributes to use consistent approach throughout the application.
- You also have option to mix the two approaches. You can specify some rules via attributes and other rules via fluent API.
Note that some configurations can be done only via attributes (for example, minimum length). Hence, this can also affect your decision.
Documentation suggests to use any one way throughout the application and then stick to it as much as possible for ensuring consistency. If any specific validation rule is not supported by selected way, then you can mix the other approach for such validations.
You can download sample code from my GitHub repository.
I hope you find this information useful. Let me know your thoughts.