Custom db provider factory while maintaining usage of SqlDatabase

Topics: Data Access Application Block
Nov 2, 2011 at 8:52 PM

I need to use a custom db provider factory (it wraps System.Data.SqlClient but adds some additional logging) in .net 3.5 I could just register it under the System.Data.SqlClient name but specify a different type. .net 4 doesn't allow this. However if I name the provider something different then the data access application block falls back to the GenericDatabase class instead of SqlDatabase and it doesn't support parameter sniffing which is also something I need. Is there a way to work arround this?

Nov 3, 2011 at 2:28 AM
Edited Nov 5, 2011 at 5:46 AM

Hi Olle,

If the logging you want to do is at the method entry and method exit I'm wondering if you could use call handlers to achieve what you want?

I think I understand what you are trying to do but if I've misinterpreted please feel free to clarify.

Here's how to go about this in code.

First create a custom DB Provider:

public class MySqlClientFactory : DbProviderFactory  
    private DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");

    public override DbConnection CreateConnection()
        Console.WriteLine("Enter: " + System.DateTime.Now);
        System.Diagnostics.Trace.TraceWarning("Creating connection");
        DbConnection conn = factory.CreateConnection();
        System.Diagnostics.Trace.TraceWarning("Got connection");
        // do something else

        return conn;

Then create a database class that extends Database (we need our own database since Database is abstract and SqlDatabase won't let you specify the DbProviderFactory):

public class MySqlDatabase : Database
    public MySqlDatabase(string connectionString, DbProviderFactory dbProviderFactory)
        : base(connectionString, dbProviderFactory)

    protected override void DeriveParameters(DbCommand discoveryCommand)

Then add MySqlDatabase to the container:

var builder = new ConfigurationSourceBuilder();
builder.ConfigureData(); // Potentially Configure DAAB

var configSource = new DictionaryConfigurationSource();
var coreExtension = new EnterpriseLibraryCoreExtension(configSource);

IUnityContainer container = new UnityContainer();

var factory = new MySqlClientFactory();

var injectionConstructor = new InjectionConstructor(
    new object[] 

container.RegisterType<Database, MySqlDatabase>("MySqlDatabase", injectionConstructor);

var configurator = new UnityContainerConfigurator(container);
IServiceLocator locator = new UnityServiceLocator(container);
EnterpriseLibraryContainer.Current = locator;

var db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MySqlDatabase");

Randy Levy
Enterprise Library support engineer

Dec 3, 2013 at 9:15 PM

I have my Database object as a dependency object injected from unity. Is there a way i can do this. I tried the same but it always returns the SqlDatabase object.
In my case i cannot directly instantiate var db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MySqlDatabase"); like this.

Any suggestions ?

Dec 5, 2013 at 5:32 AM
@madhuva, can you post more details about your application and what you are trying to achieve including code to replicate your scenario at the new Data Access Application Block site?


Randy Levy
Enterprise Library support engineer
Support How-to
Feb 25, 2014 at 7:31 AM

Can I get a sample project on how we can implement your code?