ViAMAGAZINE | ViASTUDY The Stories That Matter. Click here
Follow Me On Google News Follow Now!

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 Student, StudentAddress, 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.

Learn more about context class in the EF 6 DbContext and EF Core DbContext chapters.

Post a Comment