Entity Framework Foundation - Entity Framework Entities

What is an entity in the Entity Framework?

Entity Framework entities are classes mapped to database tables. This class must be included in the DbContext class as a DbSet < TEntity > type attribute. EF API maps each entity to a table and each attribute of the entity to a column in the database.

For example, the following Student and Grade are domain classes in 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 Grade Grade { 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; }
}   

When the above classes are included as DbSet < TEntity > attributes in the context class (classes derived from DbContext), they become entities, as shown below.

public class SchoolContext : DbContext
{
    public SchoolContext()
    {

    }

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

In the context class above, the Students and Grades attributes of the DbSet < TEntity > type are called entity sets. Students and Grades are entities. The EF API will create Students and Grades tables in the database, as shown below.


Entities can include two types of attributes: scalar attributes and navigation attributes.

Scalar attributes

Basic type attributes are called scalar attributes. Each scalar attribute is mapped to a column in a database table that stores the actual data. For example, Student ID, Student Name, DateOfBirth, Photo, Height, Weight are scalar attributes in Student entity classes.

public class Student
{
    // scalar properties
    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; }
        
    //reference navigation properties
    public Grade Grade { get; set; }
}

The EF API will create a column for each scalar attribute in the database table, as shown below.

Navigation properties

Navigation attributes represent relationships with another entity.

There are two types of navigation attributes: reference navigation and ensemble navigation.

Reference Navigation Properties

If an entity contains an attribute of another entity type, it is called a reference navigation attribute. It points to a single entity and represents the multiplicity of an entity in the entity relationship.

The EF API creates a foreign key column in the table for navigation attributes that points to the primary key of another table in the database. For example, Grade is the reference navigation attribute in the following Student entity class.

public class Student
{
    // scalar properties
    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; }
        
    //reference navigation property
    public Grade Grade { get; set; }
}

In the database, the EF API creates the foreign key Grade_GradeId in the Students table, as shown below.

Collective Navigation Properties

If an entity contains attributes of a generic set of entity types, it is called a set navigation attribute. It represents a lot of diversity.

EF API does not create any columns for collection navigation attributes in the related tables of entities, but it creates a column in the entity tables of generic collections. For example, the following Grade entity contains the generic set navigation attribute ICollection <Student>. Here, the Student entity is specified as a generic type, so the EF API will create a Grade_GradeId column in the Students table of the database.


Detailed understanding of how navigation attributes play an important role in defining entity relationships.

Tags: C# Attribute Database

Posted on Wed, 04 Sep 2019 23:13:23 -0700 by uday