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.

 

There are four public methods and two protected methods of DBContext class are very important for Database first development approach. Let's see an overview of DBContext class:

 

Public Methods:

Method Name Return Type Description Example
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 method returns DBEntityEntry object of passed entity object through which we can change its state, currentValues and originalValues.

dbCtx.Entry(StudentEntity).State = System.Data.EntityState.Modified;

dbCtx.Entry(StudentEntity).CurrentValues.SetValues(StudentEntityDTO);

dbCtx.Entry(StudentEntity).OriginalValues.SetValues(StudentEntityDTO);
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. Same as Entry method but this generic method does type check of passed EntityObject before returning DBEntityEntry object
Set(Type) DBSet Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context. CRUD operation.

ctx.Set(stud.GetType().BaseType).Add(studentEntity);
ctx.Set(stud.GetType().BaseType).Attach(studentEntity);
ctx.Set(stud.GetType().BaseType).Remove(studentEntity);

var result = ctx.Set(studentEntity.GetType().BaseType).SqlQuery("select * from student");

studentEntity.GetType() will return POCO Proxy type.
studentEntity.GetType().BaseType should be use to get an actual entity type.
Set<TEntity>() DBSet Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context. CRUD operation.

dbcontext.Set<Student>().Add(studentEntity);
dbcontext.Set<Student>().Attach(studentEntity);
dbcontext.Set<Student>().Remove(studentEntity);
var result = dbcontext.Set().SqlQuery("select * from student");
SaveChanges() int Saves all changes made in this context to the underlying database. Save changes to the database:

Dbcontext.SaveChanges();

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 change 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 of DBContext class.

 

 

Entity Framework