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

风格不统一 提交于 2019-12-05 15:56:35

问题


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 ?


回答1:


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");


来源:https://stackoverflow.com/questions/27763838/microsoft-practices-enterpriselibrary-data-dll-but-was-not-handled-in-user-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!