EF Core
Getting started with EF Core
Getting started with EF Core
  • Introduction
    • What is an ORM
    • Entity Framework Core
    • EF Core architecture
    • EF Core Approaches
  • Basics of EF Core
    • Create the project
      • Create the model & context
    • Create migration
    • CRUD using EF
      • Creating many entities
      • Update & Delete
    • Adding a property
      • Add migration
  • Entity Relationships
    • Add related entity
      • Add Vehicle
      • Modify Student
      • Modify context
      • Add migration
    • Manipulate related data
  • Entity Configuration
    • Fluent APIs
      • Frequently used APIs
    • Data Annotations
      • Frequently used annotations
Powered by GitBook
On this page
  • Use data annotations to configure a model
  • Data Annotation examples
  1. Entity Configuration

Data Annotations

Use data annotations to configure a model

You can also apply certain attributes (known as Data Annotations) to your classes and properties.

Data annotations will override conventions but will be overridden by Fluent API configuration.

(Remember: Fluent API > Annotations > Conventions)

Data Annotation examples

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

[Table("Blogs")]
public class Blog
{
    public int BlogId { get; set; }

    [Required]
    public string Url { get; set; }
}

Note the following here

  • we need a couple of name spaces in order to use data notations as shown in line 1 and 2

  • line 5 maps the Blog entity to Blogs table

  • line 10 marks the Url property as required

We can also use two annotations together on a single property, if required by business rules!

PreviousFrequently used APIsNextFrequently used annotations

Last updated 2 years ago