An entity in Entity Framework is a class that maps to a database table.
This class must be included as a DbSet<TEntity>
type property in the DbContext
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
, 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 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; } }
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<Grade> Grades { get; set; } }
In the above context class, Students
, and Grades
properties of type DbSet<TEntity>
are called entity sets.
The Student
, and Grade
are entities.
EF API will create the Students
and Grades
tables in the database, as shown below.
An Entity can include two types of properties: Scalar Properties and Navigation Properties.
The primitive type properties are called scalar properties. Each scalar property maps to a column in the database table which stores an actual data.
For example, StudentID, StudentName, DateOfBirth, Photo, Height, Weight
are the scalar properties in the 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 properties public Grade Grade { get; set; } }
EF API will create a column in the database table for each scalar property, as shown below.
The navigation property represents a relationship to another entity.
There are two types of navigation properties: Reference Navigation and Collection Navigation
If an entity includes a property of another entity type, it is called a Reference Navigation Property. It points to a single entity and represents multiplicity of one (1) in the entity relationships.
EF API will create a ForeignKey column in the table for the navigation properties that points to a PrimaryKey of another table in the database.
For example, Grade
are reference navigation properties 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, EF API will create a ForeignKey Grade_GradeId
in the Students
table, as shown below.
If an entity includes a property of generic collection of an entity type, it is called a collection navigation property. It represents multiplicity of many (*).
EF API does not create any column for the collection navigation property in the related table of an entity,
but it creates a column in the table of an entity of generic collection. For example, the following Grade
entity contains a generic collection navigation property ICollection<Student>
.
Here, the Student
entity is specified as generic type, so EF API will create a column Grade_GradeId
in the Students
table in the database.
EF API maintains the state of each entity during its lifetime. Each entity has a state based on the operation performed on it via the context class.
The entity state represented by an enum System.Data.Entity.EntityState
in EF 6 and Microsoft.EntityFrameworkCore.EntityState
in EF Core with the following values:
The Context not only holds the reference to all the entity objects as soon as retrieved from the database, but also keeps track of entity states and maintains modifications made to the properties of the entity. This feature is known as Change Tracking.
The change in entity state from the Unchanged to the Modified state is the only state that's automatically handled by the context. All other changes must be made explicitly using proper methods of DbContext
or DbSet
. (You will learn about these methods in EF 6 and EF Core sections.)
EF API builds and executes the INSERT, UPDATE, and DELETE commands based on the state of an entity when the context.SaveChanges()
method is called.
It executes the INSERT command for the entities with Added state, the UPDATE command for the entities with Modified state and the DELETE command for the entities in Deleted state.
The context does not track entities in the Detached state. The following figure illustrates the significance of entity states:
Thus, entity states play an important role in Entity Framework.