Buggy behavior in DbPRoviderFactories.GetFactoryClasses()

740 Views Asked by At

I am building an mvc app. I have this in web.config in system.data :

<system.data>
    <DbProviderFactories>
        <remove invariant ="System.Data.SqlClient" />
        <remove invariant="System.Data.Odbc" />
        <remove invariant="System.Data.Oledb" />
        <remove invariant="System.Data.OracleClient" />     

        <add name="SqlClient Data Provider" 
             invariant="System.Data.SqlClient" 
             description=".Net Framework Data Provider for SqlServer" 
             type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </DbProviderFactories>

This provider comes from a reference in the project from nuget and not in GAC. In GAC I have : enter image description here

In DemoController I have :

    DataTable table = DbProviderFactories.GetFactoryClasses();
        DbProviderFactory msSqlDbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
        var dbProviderFactories = new string[table.Rows.Count+1,table.Columns.Count+1]; //+1 for readability in display
        ushort i = 0, j = 0;
        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn column in table.Columns)
            {
                dbProviderFactories[i, j] = row[column].ToString();
                j++;
            }
            j = 0;
            i++;
        }

And as a result you can see on the screenshot.Very strange behavior of DbProviderFactories

Can anybody tell me why all the providers (ole, oracle, odbc) are still there even though I have removed them in config? Can anybody tell me why when there is no such factory as the one for SqlCleint in the array of factories I can call

DbProviderFactory msSqlDbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

and get the factory anyway? This is a strange behavior I can not explain.

I have come across the problem by doing so : 1. Initialized DbContext with default ctor when it has a connection string in web.config like this :

<connectionStrings>
        <add name="MyDbContext"
              connectionString="Data Source=MSSQL-SERVER-INSTANCE;Initial Catalog=MyDb.mdf;Trusted_Connection=False;User ID=u;Password=p"
             providerName="System.Data.SqlClient"/>
</connectionStrings>
  1. Tried to use DbMigrator with DbMigrationConfig like this :

string connectionString = "Data Source=MSSQL-SERVER-INSTANCE;Initial Catalog=MyDb.mdf ;Trusted_Connection=False;User ID=u;Password=p";

    DbMigrationsConfiguration<InitDbContext> cfg = new DbMigrationsConfiguration<InitDbContext>
                    {   
                        AutomaticMigrationsEnabled = true,
                        MigrationsAssembly = Assembly.Load("Migrations"),
                        MigrationsNamespace = "Migrations",
                        TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient")
                    };

                DbMigrationsConfiguration cfg = CreateMigrationsConfig(connectionString);
                new DbMigrator(cfg).Update();

And here comes the nasty "Unable to find the requested .Net Framework Data Provider".

The source code of DbProviderFactories

0

There are 0 best solutions below