Reputation: 4017
I have the following code
using (DbConnection conn = new Oracle.DataAccess.Client.OracleConnection(
@"<connectionString>"))
{
conn.Open();
DbProviderFactory fact = GetFactory(conn);
using (DbCommand cmd = fact.CreateCommand())
{
cmd.CommandText = "SELECT * FROM TAB";
cmd.Connection = conn;
using (DbDataAdapter dda = fact.CreateDataAdapter())
{
dda.SelectCommand = cmd;
using (DataTable dt = new DataTable("TAB"))
{
dda.Fill(dt);
}
}
}
}
The GetFactory method is this (I had to write this implementation because I'm in .NET 3.5, who it doesn't have DbProviderFactories.GetFactory(DbConnection)):
static DbProviderFactory GetFactory(DbConnection conn)
{
return DbProviderFactories.GetFactory(conn.GetType().Namespace);
}
On the following line it throws an InvalidCastException from 'Oracle.DataAccess.Client.OracleConnection' to'Oracle.DataAccess.Client.OracleConnection'.
cmd.Connection = conn;
I'm puzzled...
I'm referencing Oracle.DataAccess version 2.121.2.0
Can someone explain me what I'm missing?
Edit 1------------------
I followed the suggestion given by SLaks and I found that when
return DbProviderFactories.GetFactory(conn.GetType().Namespace);
is executed another version of Oracle.DataAccess.dll is loaded (the one on the GAC). The specific version is 2.112.3.0.
Why doesn't it use the version previously loaded?
Edit 2------------------
As SLaks said in his second suggestion I had an issue on my machine.config, it was like:
<add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
So I solved editing it in:
<add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
And installing in the GAC the Oracle.DataAccess.Client 2.121.2.0 (not necessary, but I preferred to do this)
Thanks!
Upvotes: 0
Views: 768
Reputation: 888233
This would happen if the <DbProviderFactories>
element in Machine.config references a different version of the Oracle.DataAccess assembly.
You can change either version to match, or you can add a <bindingRedirect>
in App.config.
Upvotes: 1