EF Core Configuration

Configuration enables you to override Entity Framework Core's default behaviour (conventions) in respect of mapping entities and their properties and relationships to a relational database. Configuration can be applied in two ways; through decorating classes and properties with attributes, or by using the Fluent API.

Attributes

Attributes are used to add metadata to classes, properties, methods etc in the .NET Framework. They are applied to the target class or property by placing them in square brackets (angle brackets if using VB) just before the class or property:

language-csharp
|
[MyClassAttribute]
public class MyClass
{
    [MyPropertyAttribute]
    public int MyProperty { get; set; }
    
    ...
}

You can apply multiple attributes in one of two ways - separately:

language-csharp
|
[MyFirstAttribute]
[MySecondAttribute]
[MyThirdAttribute]
public int MyProperty { get; set; }
...

or as a comma-separated list:

language-csharp
|
[MyFirstAttribute, MySecondAttribute, MyThirdAttribute]
public int MyProperty { get; set; }
...

The attributes used by EF Core reside in the System.ComponentModel.DataAnnotations namespace and are often referred to as Data Annotations.

Pros

  • Attributes are applied directly to the domain model, so it is easy to see how the model is configured just by examining the class files.
  • Some attributes, such as Required and StringLength are leveraged by client frameworks such as ASP.NET MVC to provide UI-based validation based on the specified configuration.

Cons

Data Annotation attributes can only provide a subset of configuration options. It may also be necessary to use the Fluent API to provide some configuration which will result in configuration being made in more than one place.

Fluent API

Configuration is applied to entities or their properties via chained methods, which is the identifiying feature of the Fluent API pattern.

Pros

  • The Fluent API provides greater scope for configuration than attributes.
  • Configuration can be located in one place, away from the model classes.

Cons

Really, there aren't any.

Further Reading


Date Modified: 2023-02-25
Author:

Edit this page in GitHub

Got any EF Core Question?