2012-11-21 2 views
0

machine.config를 수정하고 싶지 않지만 Npgsql 라이브러리를 데이터 공급자로 사용하려고합니다. 나는 여러 가지 플랫폼 (모노 포함)에 응용 프로그램을 배포하므로 "요청 된 .Net Framework 데이터 공급자를 찾을 수 없습니다."라는 오류 대신 "그냥 작동"하는 것이 좋습니다.런타임에 데이터 공급자를 동적으로로드하는 방법이 있습니까?

Npgsql을 런타임에 데이터 공급자로 등록하는 방법이 있습니까?

나는 Npgsql이 내 machine.config에 없어도 잘 작동한다는 것을 분명히해야한다. 그러나 NLog는 현재 나의 좌절감의 근원과 같이 잘 작동하지 않는다.

답변

5

나는 필요한 작업을 수행하는 방법을 발견했습니다. 꽤 "런타임"은 아니지만 충분히 가까워서 machine.config를 수정할 필요가 없었습니다. 이 섹션을 web.config 파일에 추가했습니다.

<system.data> 
    <DbProviderFactories> 
     <add name="Npgsql Data Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" /> 
    </DbProviderFactories> 
</system.data> 

... 그리고 그것은 챔피언처럼 작동하기 시작했습니다.

0

Reflection (코드는 Microsoft .NET Framework 용이므로 개인 필드가 Mono에서 동일한 지 확인해야합니다)을 사용해야합니다.

using System.Data.Common; 

// 1. Initialize the default configuration 
DbProviderFactories.GetFactoryClasses(); 

// 2. Retrieve the configuration table. 
var config = (DataTable)typeof(DbProviderFactories).GetField("_providerTable", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null); 

// 3. Add a new entry in the configuration (replace the values with ones for your provider). 
DataRow dataRow = config.NewRow(); 
dataRow["Name"] = "SqlClient Data Provider"; 
dataRow["InvariantName"] = typeof(SqlConnection).Namespace.ToString(); 
dataRow["Description"] = ".Net Framework Data Provider for SqlServer"; 
dataRow["AssemblyQualifiedName"] = typeof(SqlConnection).AssemblyQualifiedName; 
config.Rows.Add(dataRow); 
+0

나는 이것을 시험해 볼 것이다, 고마워. –

+0

이것은 아무 것도하지 않는 것 같습니다. 아마도 공급자 팩토리 목록을 다시 초기화하는 데 필요한 다른 명령이 있습니까? –