Monday 31 December 2018

Context Class in Entity Framework

The context class in Entity Framework is a class which derives from DbContext in EF 6 and EF Core both. It is an important class in Entity Framework, which represents a session with the underlying database.
The following SchoolContext class is an example of a context class.
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 example, the SchoolContext class is derived from DbContext, which makes it a context class. It also includes an entity set for StudentStudentAddress, and Grade entities.
The context class is used to query or save data to the database. It is also used to configure domain classes, database related mappings, change tracking settings, caching, transaction etc.

No comments:

Post a Comment