What is an Entity in Entity Framework?

An entity in Entity Framework is a class in the domain of your application which is included as a DbSet<TEntity> type property in the derived context class. EF API maps each entity to a table and each property of an entity to a column in the database.

For example, the following Student, StudentAddress and Grade are domain classes in the school application.

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    public StudentAddress StudentAddress { get; set; }
    public Grade Grade { get; set; }
}

public partial class StudentAddress
{
    public int StudentID { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    
    public Student Student { get; set; }
}

public class Grade
{
    public int GradeId { get; set; }
    public string GradeName { get; set; }
    public string Section { get; set; }

    public ICollection<Student> Students { get; set; }
}          

The above classes become entities when they are included as DbSet<TEntity> properties in a context class (the class which derives from DbContext), as shown below.

public class SchoolContext : DbContext
{
    public SchoolContext()
    {

    }

    public DbSet<Student> Students { get; set; }
    public DbSet<StudentAddress> StudentAddresses { get; set; }
    public DbSet<Grade> Grades { get; set; }
}

In the above context class, Students, StudentAddresses, and Grades properties of type DbSet<TEntity> are called entity sets. The Student, StudentAddress, and Grade are entities (also known as entity types).

An Entity can include two types of properties: Scalar Properties and Navigation Properties.

Scalar Property:

The primitive type properties are called scalar properties. A scalar property stores the actual data. a scalar property maps to a single column in the database table.

The navigation property represents a relationship to another entity, as defined by an association. They do not carry data value.

There are two types of navigation properties: Reference Navigation and Collection Navigation

Reference Navigation Property:

If an entity includes a property of entity type, it is called a Reference Navigation Property. It represents multiplicity of one (1).

Collection Navigation Property:

If an entity includes a property of collection type, it is called a collection navigation property. It represents multiplicity of many (*).

The following figure illustrates the properties of entities:

Entity Properties in Entity Framework