Skip to main content

This content relates to a pre-release version of Entity Framework (EF6)

For more information, see Future Versions


 

IDbDependencyResolver Services

Starting with EF6, the Entity Framework contains a general-purpose mechanism for obtaining implementations of services that it requires. That is, when EF uses an instance of some interfaces or base classes it will ask for a concrete implementation of the interface or base class to use. This is achieved through use of the IDbDependencyResolver interface:

public interface IDbDependencyResolver
{
    object GetService(Type type, object key);
}

 

The GetService method is typically called by EF and is handled by an implementation of IDbDependencyResolver provided either by EF or by the application. When called, the type argument is the interface or base class type of the service being requested, and the key object is either null or an object providing contextual information about the requested service.

This article does not contain full details on how to implement IDbDependencyResolver, but instead acts as a reference for the service types (i.e. interface and base class types) for which EF calls GetService and the semantics of the key object for each of these calls. This document will be kept up-to-date as additional services are added.

 

Services resolved by EF6

System.Data.Entity.IDatabaseInitializer<TContext>

Object returned: A database initializer for the given context type

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

System.Data.Entity.Migrations.Sql.MigrationSqlGenerator

Object returned: A SQL generator that can be used for Migrations and other actions that cause a database to be created, such as database creation with database initializers.

Object lifetime: Transient -- a new object should be returned each time GetService is called

Key: A string containing the ADO.NET provider invariant name specifying the type of database for which SQL will be generated. For example, the SQL Server SQL generator is returned for the key "System.Data.SqlClient".

Notes: For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Entity.Core.Common.DbProviderServices

Object returned: The EF provider to use for a given provider invariant name

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: A string containing the ADO.NET provider invariant name specifying the type of database for which a provider is needed. For example, the SQL Server provider is returned for the key "System.Data.SqlClient".

Notes: For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Entity.Infrastructure.IDbConnectionFactory

Object returned: The connection factory that will be used when EF creates a database connection by convention. That is, when no connection or connection string is given to EF, and no connection string can be found in the app.config or web.config, then this service is used to create a connection by convention. Changing the connection factory can allow EF to use a different type of database (e.g. SQL Server Compact Edition) by default.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

Notes: For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Entity.Infrastructure.IManifestTokenService

Object returned: A service that can generate a provider manifest token from a connection. This service is typically used in two ways. First, it can be used to avoid Code First connecting to the database when building a model. Second, it can be used to force Code First to build a model for a specific database version -- for example, to force a model for SQL Server 2005 even if sometimes SQL Server 2008 is used.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

System.Data.Entity.Infrastructure.IDbProviderFactoryService

Object returned: A service that can obtain a provider factory from a given connection. On .NET 4.5 the provider is publicly accessible from the connection. On .NET 4 the default implementation of this service uses some heuristics to find the matching provider. If these fail then a new implementation of this service can be registered to provide an appropriate resolution.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

System.Data.Entity.Infrastructure.IDbModelCacheKeyFactory

Object returned: A factory that will generate a model cache key for a given context. By default, EF caches one model per DbContext type per provider. A different implementation of this service can be used to add other information, such as schema name, to the cache key.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

System.Data.Entity.Spatial.DbSpatialServices

Object returned: An EF spatial provider that adds support to the basic EF provider for geography and geometry spatial types.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: DbSptialServices is asked for in two ways. First, provider-specific spatial services are requested using a DbProviderInfo object (which contains invariant name and manifest token) as the key. Second, DbSpatialServices can be asked for with no key. This is used to resolve the "global spatial provider" which is used when creating stand-alone DbGeography or DbGeometry types.

Notes: For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Entity.Infrastructure.IExecutionStrategy

Object returned: A service that allows a provider to implement retries or other behavior when queries and commands are executed against the database. If no implementation is provided, then EF will simply execute the commands and propagate any exceptions thrown. For SQL Server this service is used to provide a retry policy which is especially useful when running against cloud-based database servers such as SQL Azure.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: An ExecutionStrategyKey object that contains the provider invariant name and optionally a server name for which the execution strategy will be used.

Notes: For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Entity.Migrations.History.HistoryContextFactory

Object returned: service that allows a provider to configure the mapping of the HistoryContext to the __MigrationHistory table used by EF Migrations. The HistoryContext is a Code First DbContext and can be configured using the normal fluent API to change things like the name of the table and the column mapping specifications.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

Notes: For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Common.DbProviderFactory

Object returned: The ADO.NET provider to use for a given provider invariant name.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: A string containing the ADO.NET provider invariant name

Notes: This service is not usually changed directly since the default implementation uses the normal ADO.NET provider registration. For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Entity.Infrastructure.IProviderInvariantName

Object returned: a service that is used to determine a provider invariant name for a given type of DbProviderFactory. The default implementation of this service uses the ADO.NET provider registration. This means that if the ADO.NET provider is not registered in the normal way because DbProviderFactory is being resolved by EF, then it will also be necessary to resolve this service.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: The DbProviderFactory instance for which an invariant name is required.

Notes: For more details on provider-related services in EF6 see the EF6 provider model documentation on CodePlex.

System.Data.Entity.Core.Mapping.ViewGeneration.IViewAssemblyCache

Object returned: a cache of the assemblies that contain pre-generated views. A replacement is typically used to let EF know which assemblies contain pre-generated views without doing any discovery.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

System.Data.Entity.Infrastructure.Pluralization.IPluralizationService

Object returned: a service used by EF to pluralize and singularize names. By default an English pluralization service is used.

Object lifetime: Singleton -- the same object may be used multiple times and concurrently by different threads

Key: Not used; will be null

Microsoft is conducting an online survey to understand your opinion of the MSDN Web site. If you choose to participate, the online survey will be presented to you when you leave the MSDN Web site.

Would you like to participate?