enterprise-library

Microsoft.Practices.EnterpriseLibrary.Data.DLL but was not handled in user code


Searched google and using Enterprise library data access to connect database.

Installed only data access pack using https://www.nuget.org/packages/EnterpriseLibrary.Data/.

After added to the project, I've set the configuration as follows,

     <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    </configSections>
  <dataConfiguration defaultDatabase="dProvider" />
    <connectionStrings>
        <add name="dProvider" connectionString="server=local;Initial Catalog=n;uid=sa;pwd=pwd"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

Called through the application like the following,

Database db;
            string sqlCommand;
            DbCommand dbCommand;

            db = DatabaseFactory.CreateDatabase("dProvider"); or DatabaseFactory.CreateDatabase();

After run the application, I got the following exception,

{"Database provider factory not set for the static DatabaseFactory. Set a provider factory invoking the DatabaseFactory.SetProviderFactory method or by specifying custom mappings by calling the DatabaseFactory.SetDatabases method."}

What mistake I made ? How to solve this issue ?


Solution

  • Finally found the answer. It has been occurred because of the configuration section.

    I've used version 6, but here I've mentioned like version 5 in the configuration section. So the error has occurred.

    I've replaced the configuration section like following, It worked perfectly in good way. :-). Thanks a lot for the helpers.

    <configSections>
             <section name="dataConfiguration" 
          type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, 
                Microsoft.Practices.EnterpriseLibrary.Data"/>
        </configSections>
    

    and used DataBaseProviderFactory class to create instance.

    DatabaseProviderFactory factory = new DatabaseProviderFactory();
    
                db = factory.Create("dProvider");