Add a DbProviderFactory without an App.Config

前端 未结 8 1778
无人共我
无人共我 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条回答
  •  猫巷女王i
    2020-12-13 07:12

    Even Later Answer

    Get it using configuration like above. I've found that this seems to require the provider assembly to be somewhere that the running program can find it.

        /// 
        /// Creates a DbProviderFactory instance without needing configuration file
        /// 
        /// Name of the provider.  Like "System.Data.SQLite"
        /// Class and assembly information.  Like "System.Data.SQLite.SQLiteFactory, System.Data.SQLite"
        /// A specific DbProviderFactory instance, or null if one can't be found
        protected static DbProviderFactory GetDbProviderFactoryFromConfigRow(string lsProviderName, string lsClass)
        {
            if (string.Empty != lsProviderName && string.Empty != lsClass)
            {
                DataRow loConfig = null;
                DataSet loDataSet = ConfigurationManager.GetSection("system.data") as DataSet;
                foreach (DataRow loRow in loDataSet.Tables[0].Rows)
                {
                    if ((loRow["InvariantName"] as string) == lsProviderName)
                    {
                        loConfig = loRow;
                    }
                }
    
                if (null == loConfig)
                {
                    loConfig = loDataSet.Tables[0].NewRow();
                    loConfig["InvariantName"] = lsProviderName;
                    loConfig["Description"] = "Dynamically added";
                    loConfig["Name"] = lsProviderName + "Name";
                    loConfig["AssemblyQualifiedName"] = lsClass;
                    loDataSet.Tables[0].Rows.Add(loConfig);
                }
    
                try
                {
                    DbProviderFactory loDbProviderFactoryByRow = DbProviderFactories.GetFactory(loConfig);
                    return loDbProviderFactoryByRow;
                }
                catch (Exception loE)
                {
                    //// Handled exception if needed, otherwise, null is returned and another method can be tried.
                }
            }
    

    Another method that gets the Instance field directly from the assembly. It works even when the DLL is somewhere else on the system.

        /// 
        /// Creates a DbProviderFactory instance without needing configuration file
        /// 
        /// Class and assembly information.  Like "System.Data.SQLite.SQLiteFactory, System.Data.SQLite"
        /// Full path to the assembly DLL. Like "c:\references\System.Data.SQLite.dll"
        /// A specific DbProviderFactory instance, or null if one can't be found
        protected static DbProviderFactory GetDbProviderFactoryFromAssembly(string lsClass, string lsAssemblyFile)
        {
            if (lsAssemblyFile != string.Empty && lsClass != string.Empty)
            {
                Assembly loAssembly = System.Reflection.Assembly.LoadFrom(lsAssemblyFile);
                if (null != loAssembly)
                {
                    string[] laAssembly = lsClass.Split(new char[] { ',' });
                    Type loType = loAssembly.GetType(laAssembly[0].Trim());
                    FieldInfo loInfo = loType.GetField("Instance");
                    if (null != loInfo)
                    {
                        object loInstance = loInfo.GetValue(null);
                        if (null != loInstance)
                        {
                            if (loInstance is System.Data.Common.DbProviderFactory)
                            {
                                return loInstance as DbProviderFactory;
                            }
                        }
                    }
                }
            }
    
            return null;
        }
    

提交回复
热议问题