3 out of 9 rated this helpful - Rate this topic

DbContext Class

Entity Framework

Provides facilities for querying and working with entity data as objects.

System.Object
  System.Data.Entity.DbContext

Namespace:  System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)
public class DbContext : IDisposable, IObjectContextAdapter

The DbContext type exposes the following members.

  Name Description
Protected method 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.
Public method 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.
Protected method 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.
Public method 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.
Public method DbContext(ObjectContext, Boolean) Constructs a new context instance around an existing ObjectContext.
Public method 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.
Public method 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.
Top
  Name Description
Public property ChangeTracker Provides access to features of the context that deal with change tracking of entities.
Public property Configuration Provides access to configuration options for the context.
Public property Database Creates a database instance for this context and allows you to perform creation, deletion or existence checks for the underlying database.
Top
  Name Description
Public method Dispose() Calls the protected Dispose method.
Protected 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.
Public method 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.
Public method Entry<TEntity>(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.
Public method Equals Returns whether the specified context is equal to the current context. (Overrides Object.Equals(Object).)
Protected method Finalize (Inherited from Object.)
Public method GetHashCode Returns the hash function for the specified context. (Overrides Object.GetHashCode().)
Public method GetType Gets the type for the current context.
Public method GetValidationErrors Validates tracked entities and returns a Collection of DbEntityValidationResult containing validation results.
Protected method MemberwiseClone (Inherited from Object.)
Protected method 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.
Public method SaveChanges Saves all changes made in this context to the underlying database.
Public method Set(Type) Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context.
Public method Set<TEntity>() Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context.
Protected method ShouldValidateEntity Extension point allowing the user to override the default behavior of validating only added and modified entities.
Public method ToString Returns a string representation of the context. (Overrides Object.ToString().)
Protected method ValidateEntity Extension point allowing the user to customize validation of an entity or filter out validation results. Called by GetValidationErrors().
Top
  Name Description
Explicit interface implemetation Private property IObjectContextAdapter.ObjectContext Returns the Entity Framework ObjectContext that is underlying this context.
Top

DbContext wraps ObjectContext and exposes the most commonly used features of ObjectContext by using simplified and more intuitive APIs. You can access the underlying ObjectContext whenever you need to use features that are not supported by DbContext. For more information, see What’s Not Supported.

DbContext is usually used with a derived type that contains DbSet<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 Entity Data Model backing the context can be specified in several ways. When using the Code First approach, the DbSet<TEntity> properties on the derived context are used to build a model by convention. The protected OnModelCreating(DbModelBuilder) method can be overridden to modify this model. More granular control over the model used for the Model First approach can be obtained by creating a DbCompiledModel explicitly from a DbModelBuilder and passing this model to one of the DbContext constructors. When using the Database First or Model First approach the Entity Data Model can be created using the Entity Designer (or manually by creating an EDMX file) and then this model can be specified using entity connection string or an EntityConnection object. The connection to the database (including the name of the database) can be specified in several ways. If the default DbContext constructor is called from a derived context, then the name of the derived context is used to find a connection string in the app.config or web.config file. If no connection string is found, then the name is passed to the DefaultConnectionFactory registered on the Database class. The connection factory then uses the context name as the database name in a default connection string. (This default connection string points to .\SQLEXPRESS on the local machine unless a different DefaultConnectionFactory is registered.) Instead of using the derived context name, the connection/database name can also be specified explicitly by passing the name to one of the DbContext constructors that takes a string. The name can also be passed in the form "name=myname", in which case the name must be found in the config file or an exception will be thrown.

The connection found in the app.config or web.config file can be a normal database connection string (not a special Entity Framework connection string) in which case the DbContext will use Code First. However, if the connection found in the configuration file is a special Entity Framework connection string, then the DbContext will use Database/Model First and the model specified in the connection string will be used. An existing or explicitly created DbConnection can also be used instead of the database/connection name. A DbModelBuilderVersionAttribute can be applied to a class derived from DbContext to set the version of conventions used by the context when it creates a model. If no attribute is applied then the latest version of conventions will be used.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)