DBContext Class

DBContext represents a combination of the Unit-Of-Work and Repository patterns and enables you to query a database, and group together changes that will then be written back to the store as a unit.

 

The table shown below shows the important methods of DBContext class.

 

Public Methods:

Method Name Return Type Description
Entry(Object) DBEntityEntry Gets a DbEntityEntry object for the given entity, providing access to information about the entity and the ability to perform actions on the entity.
Entry <TEntity>(TEntity) DbEntityEntry<TEntity> Gets a DbEntityEntry<TEntity> object for the given entity, providing access to information about the entity and the ability to perform actions on the entity.
Set(Type) DBSet Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context.
Set<TEntity>() DBSet Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context.
SaveChanges() int Saves all changes made in this context to the underlying database.

Protected Methods:

Method Name Return Type Description
ShouldValidateEntity bool Extension point allowing the user to override the default behavior of validating, only added and modified entities.
ValidateEntity DbEntityValidationResult Extension point allowing the user to customize validation of an entity or filter out validation results. Called by GetValidationErrors().

Public Properties:

Property Name Return Type Description
ChangeTracker DBChangeTracker Provides access to features of the context that deal with changing tracking of entities.
Configuration DBContextConfiguration Provides access to configuration options for the context.
Database Database Creates a database instance for this context and allows you to perform creation, deletion or existence checks for the underlying database.

 

Visit MSDN for more information on DBContext Class.