Add a DbProviderFactory without an App.Config

前端 未结 8 1779
无人共我
无人共我 2020-12-13 06:52

I am using DbProviderFactories in my data layer (based on Entity Framework) and am using SQLite for my database, but I don\'t have to have a App.Config to have the following

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 07:12

    JoshRivers above posted a solution for SQLite. This can in fact be used for other adapters as well- I was able to get it working for MySQL using his example. I have wrapped this into something a bit more generic. This should be run once the application starts and is for the .NET connector version 6.6.5.0 (but I imagine it is good for other versions as well.)

    string dataProvider = @"MySql.Data.MySqlClient";
    string dataProviderDescription = @".Net Framework Data Provider for MySQL";
    string dataProviderName = @"MySQL Data Provider";
    string dataProviderType = @"MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d";
    
    bool addProvider = true;
    var dataSet = ConfigurationManager.GetSection("system.data") as DataSet;
    foreach (DataRow row in dataSet.Tables[0].Rows)
    {
        if ((row["InvariantName"] as string) == dataProvider)
        {
            // it is already in the config, no need to add.
            addProvider = false;
            break;
        }
    }
    
    if (addProvider)
        dataSet.Tables[0].Rows.Add(dataProviderName, dataProviderDescription, dataProvider, dataProviderType);
    

提交回复
热议问题