Configuration In Entity Framework Core

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:

[MyClassAttribute]
public class MyClass
{
    [MyPropertyAttribute]
    public int MyProperty { get; set; }
    
    ...
}

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

[MyFirstAttribute]
[MySecondAttribute]
[MyThirdAttribute]
public int MyProperty { get; set; }
...

or as a comma-separated list:

[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.

Further Reading


Created:
Last updated: 14/03/2017 16:33:40
Proficiency Level: Beginner