DbPRoviderFactories.GetFactoryClasses()中的越野行为

时间:2013-07-23 08:04:17

标签: dbproviderfactories

我正在构建一个mvc应用程序。我在system.data中的web.config中有这个:

<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>

此提供程序来自nuget的项目参考,而非GAC。在GAC中,我有:enter image description here

在DemoController中我有:

    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++;
        }

因此,您可以在屏幕截图中看到。Very strange behavior of DbProviderFactories

有人能告诉我为什么所有的提供者(ole,oracle,odbc)仍然在那里,即使我已经在配置中删除了它们? 任何人都可以告诉我为什么在我可以调用的工厂数组中没有像SqlCleint那样的工厂时

DbProviderFactory msSqlDbProviderFactory = DbProviderFactories.GetFactory(&#34; System.Data.SqlClient&#34;);

然后到工厂去?这是一个我无法解释的奇怪行为。

我通过这样做遇到了这个问题:  1.当web.config中有连接字符串时,使用默认ctor初始化DbContext,如下所示:

<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. 尝试将DbMigrator与DbMigrationConfig一起使用,如下所示:
  2. string connectionString =&#34; Data Source = MSSQL-SERVER-INSTANCE; Initial Catalog = MyDb.mdf; Trusted_Connection = False; User ID = u; Password = p&#34 ;;

        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();
    

    这是令人讨厌的&#34;无法找到所请求的.Net框架数据提供商&#34;。

    The source code of DbProviderFactories

0 个答案:

没有答案