Exemplo n.º 1
0
        /// <summary>
        /// This method loads various providers dynamically similar to the
        /// way that DbProviderFactories.GetFactory() works except that
        /// this API is not available on .NET Standard 2.0
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DbProviderFactory GetSqlProviderFactory(DataAccessProviderTypes type)
        {
            if (type == DataAccessProviderTypes.SqlServer)
            {
                return(SqlClientFactory.Instance);
            }

            if (type == DataAccessProviderTypes.SqLite)
            {
#if NETCORE
                var instance = ReflectionUtils.GetStaticProperty("Microsoft.Data.Sqlite.SqliteFactory", "Instance");
                if (instance == null)
                {
                    var a = ReflectionUtils.LoadAssembly("Microsoft.Data.Sqlite");
                    if (a != null)
                    {
                        instance = ReflectionUtils.GetStaticProperty("Microsoft.Data.Sqlite.SqliteFactory", "Instance");
                    }
                }

                if (instance == null)
                {
                    throw new InvalidOperationException("Couldn't load SqLite Provider factory. Please make sure the Microsoft.Data.Sqlite package has been added to your project");
                }

                return(instance as DbProviderFactory);
#else
                var instance = ReflectionUtils.GetStaticProperty("System.Data.Sqlite.SQLiteFactory", "Instance");
                if (instance == null)
                {
                    var a = ReflectionUtils.LoadAssembly("System.Data.SQLite");
                    if (a != null)
                    {
                        instance = ReflectionUtils.GetStaticProperty("System.Data.SQLite.SQLiteFactory", "Instance");
                    }
                }

                if (instance == null)
                {
                    throw new InvalidOperationException(
                              "Couldn't load SqLite Provider factory. Please make sure the System.Data.SQLite reference has been added to your project");
                }
                return(instance as DbProviderFactory);
#endif
            }

            throw new InvalidOperationException("Unsupported Provider Factory specified: " + type);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads a SQL Provider factory based on the DbFactory type name and assembly.
        /// </summary>
        /// <param name="dbProviderFactoryTypename">Type name of the DbProviderFactory</param>
        /// <param name="assemblyName">Short assembly name of the provider factory. Note: Host project needs to have a reference to this assembly</param>
        /// <returns></returns>
        public static DbProviderFactory GetDbProviderFactory(string dbProviderFactoryTypename, string assemblyName)
        {
            var instance = ReflectionUtils.GetStaticProperty(dbProviderFactoryTypename, "Instance");

            if (instance == null)
            {
                var a = ReflectionUtils.LoadAssembly(assemblyName);
                if (a != null)
                {
                    instance = ReflectionUtils.GetStaticProperty(dbProviderFactoryTypename, "Instance");
                }
            }

            if (instance == null)
            {
                throw new InvalidOperationException(string.Format(Resources.UnableToRetrieveDbProviderFactoryForm, dbProviderFactoryTypename));
            }

            return(instance as DbProviderFactory);
        }