New DbContext API in Entity Framework 4.1



DbContext - Entity Framework 4.1

Note: Using “DbContext” without installed Entity Framework 4.1 and Entity Framework 4.0 is impossible.

April 2011 saw the release of a new version of Entity Framework 4.1 that supports DbContext API and fluent mapping. Initially, fluent mapping was presumed to be used in the Code-First concept but now it works good with the DataBase-First and Model-First approaches as well.

DbContext

Entity Framework 4.1 acquires the major novelty called DbContext. Being an ObjectContext wrapper, this is a more lightweight alternative to the use of ObjectContext in Entity Framework v.1 and Entity Framework v 4.0. DbContext can be used with XML-mapping and fluent mapping.

DbContext Class

Inheritance Hierarchy

System..::..Object  

System.Data.Entity..::..DbContext



Namespace:  System.Data.Entity

Assembly:  EntityFramework (in EntityFramework.dll)

The DbContext type exposes the following members.

 

Constructors

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(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 forDbContext.

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.

Properties

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.

For more information on DbContext, use the MSDN documentation.

Fluent mapping

The DbContext template of Entity Developer has the FluentMapping option determining, if the Entity Framework code only mapping is appended into the XML mapping or generated code from the edml file is used. By default the value is false, it means that XML mapping is used.

In preference, if FluentMapping is set to True, fluent mapping is generated. This process generates the primary key for tables, column facets, complex types, foreign keys taking into account inheritance of all three types (TPH, TPT, and TPC) and simple entity splitting.

Find a brief example of generated mapping for a simple model consisting of three classes with TPH below:

dbContext Fluent Mapping



Code-First Configuration Conventions

The DbContext template of Entity Developer contains the DisabledConventions option allowing you to disable the configuration conventions of Code-First.

The Entity Framework Code-First list of conventions is too large, that’s why our DisabledConventions list does not include various attribute conventions (for example, ColumnAttributeConvention, ComplexTypeAttributeConvention and others) that the user normally does not need to disable.

One of the most useful conventions that gets disabled more often than not is IncludeMetadataConvention. This convention determines whether it is necessary to create / use / delete the “EdmMetadata” table that stores the model hash which is used to check whether the model has changed since the database was created from it.

Simultaneous Use of Several Databases

Previously, there was only one way of creating an EF-application that uses several databases, for example, Oracle + Microsoft SQL Server. First, we created several EF-models, one model per DBMS. Then we put the following resources into the application: one CSDL, one MSL and one SSDL per each database. Then, depending on the server, we changed the connection string that specified different resources.

Fluent mapping is a real alternative to the approach involving several EF-models and different SSDL-resources. Now it is enough to create one EF-model in the Entity Developer application using the Database-First or Model-First approach. Following that, we remove the standard code generation template and enable the DbContext template. In the template properties, we need to set FluentMapping=True andDatabaseIndependent=True.

The DatabaseIndependent property determines whether the database-specific data type is set when fluent mapping is generated. If DatabaseIndependent is set by default to False, the following code is generated:

DatabaseIndependent



Top of Page

Join Splash Magazines
Feature Article

Tempflow™ and Tempur-Pedic® Reviews - What 35 Hours of Research Uncovered

Want Your Business to Male a Splash