Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First - Advantages and disadvantages of Fluent Api vs Data Annotations [closed]

People also ask

Is Fluent API better than data annotation attributes?

The fluent API is considered a more advanced feature and we would recommend using Data Annotations unless your requirements require you to use the fluent API. But in my opinion you reach the limitations of DataAnnotations very quickly (except perhaps for extremely simple object models).

What is the advantage and disadvantage of entity framework?

Easy to map business objects (with drag & drop tables on environment). It keeps a good performance when you work with a small / middle domain model. Disadvantages: You have to think in a non-traditional way of handling data , not available for every database.

Does data annotation attributes override Fluent API configuration?

To write Fluent API configurations, override the OnModelCreating() method of DbContext in a context class, as shown below. You can use Data Annotation attributes and Fluent API at the same time. Entity Framework gives precedence to Fluent API over Data Annotations attributes.

When should I use Fluent API?

Fluent API is another way to configure your domain classes. The Code First Fluent API is most commonly accessed by overriding the OnModelCreating method on your derived DbContext. Fluent API provides more functionality for configuration than DataAnnotations.


Everything what you can configure with DataAnnotations is also possible with the Fluent API. The reverse is not true. So, from the viewpoint of configuration options and flexibility the Fluent API is "better".

Configuration examples (for sure not a full list) which are possible in the Fluent API but not with DataAnnotations (as far as I can see):

  • Switch off cascading deletes:

    .WillCascadeOnDelete(false)

  • Specify foreign key column name in the database when the key isn't exposed in your object model:

    .Map(conf => conf.MapKey("MyForeignKeyID"))

  • Fine granular tuning of relationships, especially in all cases where only one side of an association is exposed in the object model:

    .WithMany(...), WithOptional(...), WithRequiredDependent(...), WithRequiredPrincipal(...)

  • Specification of inheritance mapping between object model and database tables (Table-Per-Hierarchy, Table-Per-Type, Table-Per-Concrete-Class):

    .Map<TDerived>(Action<EntityMappingConfiguration<TDerived>> ...)

Edit: Microsoft considers the Fluent API as an "advanced feature" (Quote from here):

The fluent API is considered a more advanced feature and we would recommend using Data Annotations unless your requirements require you to use the fluent API.

But in my opinion you reach the limitations of DataAnnotations very quickly (except perhaps for extremely simple object models). If you cannot fine tune your model with DataAnnotations anymore your last resort is to follow the default mapping conventions (by naming your properties according to those rules). Currently you cannot overwrite the conventions (only disable them; MS announced to give configuration options for the conventions in future EF releases). But if you don't want to be forced by the mapping conventions when you define your object model, your only option then is the Fluent API.

Learning the Fluent API is almost a Must imho, the DataAnnotations are a nice-to-have for simple applications.