0

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!

5
  • 1
    It sounds like you have multiple versions of the assembly loaded. Check Debug, Modules. Commented Mar 12, 2015 at 13:45
  • I agree with SLaks. Check your references. I have also seen similar issues when missing dependencies. Commented Mar 12, 2015 at 13:59
  • This is probably a machine.config issue. Commented Mar 12, 2015 at 13:59
  • Why do you use DbProviderFactory? Does your application have to work with different database types (and database providers)? Commented Mar 12, 2015 at 14:45
  • 1
    @Wernfried Yes, the example i wrote was minimal to facilitate the comprehension Commented Mar 12, 2015 at 14:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.