DbContext Class
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. DbContext is conceptually similar to ObjectContext.
Namespace: System.Data.Entity
Assembly: EntityFramework (in EntityFramework.dll)
The DbContext type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | DbContext | Constructs a new context instance using conventions to create the name of the database to which a connection will be made. By convention the name is the full name (namespace + class name) of the derived context class. For more information on how this is used to create a connection, see the remarks section for DbContext. |
![]() | DbContext(String) | Constructs a new context instance using the given string as the name or connection string for the database to which a connection will be made. For more information on how this is used to create a connection, see the remarks section for DbContext. |
![]() | DbContext(DbCompiledModel) | Constructs a new context instance using conventions to create the name of the database to which a connection will be made, and initializes it from the given model. By convention the name is the full name (namespace + class name) of the derived context class. For more information on how this is used to create a connection, see the remarks section for DbContext. |
![]() | DbContext(DbConnection, Boolean) | Constructs a new context instance using the existing connection to connect to a database. The connection will not be disposed when the context is disposed. |
![]() | DbContext(ObjectContext, Boolean) | Constructs a new context instance around an existing ObjectContext. |
![]() | DbContext(String, DbCompiledModel) | Constructs a new context instance using the given string as the name or connection string for the database to which a connection will be made, and initializes it from the given model. For more information on how this is used to create a connection, see the remarks section for DbContext. |
![]() | DbContext(DbConnection, DbCompiledModel, Boolean) | Constructs a new context instance using the existing connection to connect to a database, and initializes it from the given model. The connection will not be disposed when the context is disposed. |
| Name | Description | |
|---|---|---|
![]() | ChangeTracker | Provides access to features of the context that deal with change tracking of entities. |
![]() | Configuration | Provides access to configuration options for the context. |
![]() | Database | Creates a database instance for this context and allows you to perform creation, deletion or existence checks for the underlying database. |
| Name | Description | |
|---|---|---|
![]() | Dispose | Calls the protected Dispose method. |
![]() | Dispose(Boolean) | Disposes the context. The underlying ObjectContext is also disposed if it was created is by this context or ownership was passed to this context when this context was created. The connection to the database (DbConnection object) is also disposed if it was created is by this context or ownership was passed to this context when this context was created. |
![]() | Entry(Object) | 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(Of TEntity)(TEntity) | Gets a DbEntityEntry(Of TEntity) object for the given entity providing access to information about the entity and the ability to perform actions on the entity. |
![]() | Equals | Returns whether the specified context is equal to the current context. (Overrides Object.Equals(Object).) |
![]() | Finalize | (Inherited from Object.) |
![]() | GetHashCode | Returns the hash function for the specified context. (Overrides Object.GetHashCode.) |
![]() | GetType | Gets the type for the current context. |
![]() | GetValidationErrors | Validates tracked entities and returns a Collection of DbEntityValidationResult containing validation results. |
![]() | MemberwiseClone | (Inherited from Object.) |
![]() | OnModelCreating | This method is called when the model for a derived context has been initialized, but before the model has been locked down and used to initialize the context. The default implementation of this method does nothing, but it can be overridden in a derived class such that the model can be further configured before it is locked down. |
![]() | SaveChanges | Saves all changes made in this context to the underlying database. |
![]() | Set(Type) | Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context. |
![]() | Set(Of TEntity) | Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context. |
![]() | ShouldValidateEntity | Extension point allowing the user to override the default behavior of validating only added and modified entities. |
![]() | ToString | Returns a string representation of the context. (Overrides Object.ToString.) |
![]() | ValidateEntity | Extension point allowing the user to customize validation of an entity or filter out validation results. Called by GetValidationErrors. |
| Name | Description | |
|---|---|---|
![]() ![]() | IObjectContextAdapter.ObjectContext | Returns the Entity Framework ObjectContext that is underlying this context. |
DbContext is most commonly used with a derived type that contains DbSet(Of TEntity) properties for the root entities of the model. These sets are automatically initialized when the instance of the derived class is created. This behavior can be modified by applying the SuppressDbSetInitializationAttribute attribute to either the entire derived context class, or to individual properties on the class.
The conceptual model that is associated with the context can be specified in several ways. When using the Code First approach, the DbSet(Of TEntity) properties on the derived context are used to build the model by convention. The protected OnModelCreating(DbModelBuilder) method can be overridden to modify this model. More granular control over the creation of the model can be obtained by creating a DbCompiledModel explicitly from a DbModelBuilder and passing this model to one of the DbContext constructors. When using the Entity Framework Designer to create the conceptual model, the EntityConnection connection string must be used to specify which model and mapping files to associate with the context. By default, the Entity Designer creates this connection string and adds it to the App.config or Web.config file. For information about how the model is created or cached, see Code First Building Blocks.
For information about how to specify the connection, see Managing Connections and Transactions.

