LinqToDB4iSeries / Linq2DB4iSeries

LinqToDB provider for the DB2 database on IBM iSeries
MIT License
18 stars 5 forks source link

[Question] Autodetect provider issue #78

Closed robtomlinson closed 3 months ago

robtomlinson commented 3 months ago

In my organization, we have an internal Nuget package that uses Linq2DB4iSeries. We are updating to the latest version, and it appears the DB2iSeriesProviderDetector was changed to be internal scope in 2023. So, our previous implementation which used this in the setup is no longer working: public DB2Connection(string connectionString) : base(new DB2iSeriesProviderDetector().AutoDetectDataProvider(connectionString), connectionString) { }

I have updated this to now use the internal method, like this: public DB2Connection(string connectionString) : base(SetupConnection(connectionString), connectionString) { } With the helper method:

private static IDataProvider SetupConnection(string connectionString)
{
    // Auto-detect the iSeries provider from the connection string
    var dataProvider = DB2iSeriesTools.ProviderDetector(new ConnectionOptions
    {
        ConnectionString = connectionString
    });

    return dataProvider;
}

But, when I attempt to use this connection, I am getting an object null reference from Linq2DB4iSeries. What am I doing wrong here? Are there any examples or documentation of how to use the auto detect provider now that it has had its scope changed to internal? We need to support various connection methods: ODBC, OLE, etc. and I'm trying to not have to set the provider to a specific type or require adding the type as a parameter which would require some refactoring in our solution that uses this nuget package.

robtomlinson commented 3 months ago

For reference, here is the entire class we're using:

using SomeApp.DB2.Entities;
using LinqToDB;
using LinqToDB.Data;
using LinqToDB.DataProvider;
using LinqToDB.DataProvider.DB2iSeries;

namespace SomeApp.DB2.Connections
{
    public class DB2Connection : LinqToDB.Data.DataConnection
    {
        public DB2Connection(string connectionString) : base(SetupConnection(connectionString), connectionString) { }

        public ITable<SomeTable> SomeTable => this.GetTable<SomeTable>();

        private static IDataProvider SetupConnection(string connectionString)
        {
            // Auto-detect the iSeries provider from the connection string
            var dataProvider = DB2iSeriesTools.ProviderDetector(new ConnectionOptions
            {
                ConnectionString = connectionString
            });

            return dataProvider;
        }
    }
}
robtomlinson commented 3 months ago

I was able to solve this issue by altering the static helper method to the following:

private static IDataProvider SetupConnection(string connectionString)
{
    // Auto-detect the iSeries provider from the connection string
    var dataProvider = DB2iSeriesTools.ProviderDetector(new ConnectionOptions
    {
        ConnectionString = connectionString,
        ProviderName = DB2iSeriesProviderName.DB2
    });

    return dataProvider;
}