Export (0) Print
Expand All

DbContext Class

Entity Framework 5.0

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.

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.

  NameDescription
Protected methodDbContext()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 methodDbContext(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 methodDbContext(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 methodDbContext(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 methodDbContext(ObjectContext, Boolean)Constructs a new context instance around an existing ObjectContext.
Public methodDbContext(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 methodDbContext(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

  NameDescription
Public propertyChangeTrackerProvides access to features of the context that deal with change tracking of entities.
Public propertyConfigurationProvides access to configuration options for the context.
Public propertyDatabaseCreates a database instance for this context and allows you to perform creation, deletion or existence checks for the underlying database.
Top

  NameDescription
Public methodDispose()Calls the protected Dispose method.
Protected methodDispose(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 methodEntry(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 methodEntry<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 methodEqualsReturns whether the specified context is equal to the current context. (Overrides Object.Equals(Object).)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCodeReturns the hash function for the specified context. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the type for the current context.
Public methodGetValidationErrorsValidates tracked entities and returns a Collection of DbEntityValidationResult containing validation results.
Protected methodMemberwiseClone (Inherited from Object.)
Protected methodOnModelCreatingThis 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 methodSaveChangesSaves all changes made in this context to the underlying database.
Public methodSet(Type)Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context.
Public methodSet<TEntity>()Returns a DbSet for the specified type, this allows CRUD operations to be performed for the given entity in the context.
Protected methodShouldValidateEntityExtension point allowing the user to override the default behavior of validating only added and modified entities.
Public methodToStringReturns a string representation of the context. (Overrides Object.ToString().)
Protected methodValidateEntityExtension point allowing the user to customize validation of an entity or filter out validation results. Called by GetValidationErrors().
Top

  NameDescription
Explicit interface implemetationPrivate propertyIObjectContextAdapter.ObjectContextReturns the Entity Framework ObjectContext that is underlying this context.
Top

DbContext is most commonly 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 conceptual model that is associated with 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 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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft