diff --git a/XCode/DataAccessLayer/Common/DbBase.cs b/XCode/DataAccessLayer/Common/DbBase.cs index 5658921e11..66523acd55 100644 --- a/XCode/DataAccessLayer/Common/DbBase.cs +++ b/XCode/DataAccessLayer/Common/DbBase.cs @@ -306,64 +306,74 @@ public IMetaData CreateMetaData() /// 获取提供者工厂 /// /// + /// /// - protected static DbProviderFactory GetProviderFactory(String assemblyFile, String className) + public static DbProviderFactory GetProviderFactory(String assemblyFile, String className, Boolean ignoreError = false) { - var name = Path.GetFileNameWithoutExtension(assemblyFile); - var linkName = name; - if (Runtime.Is64BitProcess) linkName += "64"; - var ver = Environment.Version; - if (ver.Major >= 4) linkName += "Fx" + ver.Major + ver.Minor; - // 有些数据库驱动不区分x86/x64,并且逐步以Fx4为主,所以来一个默认 - linkName += ";" + name; + try + { + var name = Path.GetFileNameWithoutExtension(assemblyFile); + var linkName = name; + if (Runtime.Is64BitProcess) linkName += "64"; + var ver = Environment.Version; + if (ver.Major >= 4) linkName += "Fx" + ver.Major + ver.Minor; + // 有些数据库驱动不区分x86/x64,并且逐步以Fx4为主,所以来一个默认 + linkName += ";" + name; #if __CORE__ - linkName = "st_" + name; - if (!name.IsNullOrEmpty()) - { - className = className + "," + name;//指定完全类型名可获取项目中添加了引用的类型,否则dll文件需要放在根目录 - } + linkName = "st_" + name; + if (!name.IsNullOrEmpty()) + { + className = className + "," + name;//指定完全类型名可获取项目中添加了引用的类型,否则dll文件需要放在根目录 + } #endif - var type = PluginHelper.LoadPlugin(className, null, assemblyFile, linkName); - - // 反射实现获取数据库工厂 - var file = assemblyFile; - var plugin = NewLife.Setting.Current.GetPluginPath(); - file = plugin.CombinePath(file); + var type = PluginHelper.LoadPlugin(className, null, assemblyFile, linkName); - // 如果还没有,就写异常 - if (type == null) - { - if (assemblyFile.IsNullOrEmpty()) return null; - if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file); - } - - if (type == null) - { - XTrace.WriteLine("驱动文件{0}无效或不适用于当前环境,准备删除后重新下载!", assemblyFile); + // 反射实现获取数据库工厂 + var file = assemblyFile; + var plugin = NewLife.Setting.Current.GetPluginPath(); + file = plugin.CombinePath(file); - try + // 如果还没有,就写异常 + if (type == null) { - File.Delete(file); + if (assemblyFile.IsNullOrEmpty()) return null; + if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file); } - catch (UnauthorizedAccessException) { } - catch (Exception ex) { XTrace.Log.Error(ex.ToString()); } - type = PluginHelper.LoadPlugin(className, null, file, linkName); + if (type == null) + { + XTrace.WriteLine("驱动文件{0}无效或不适用于当前环境,准备删除后重新下载!", assemblyFile); - // 如果还没有,就写异常 - if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file); - } - if (type == null) return null; + try + { + File.Delete(file); + } + catch (UnauthorizedAccessException) { } + catch (Exception ex) { XTrace.Log.Error(ex.ToString()); } - var asm = type.Assembly; - if (DAL.Debug) DAL.WriteLog("{2}驱动{0} 版本v{1}", asm.Location, asm.GetName().Version, name ?? className.TrimEnd("Client", "Factory")); + type = PluginHelper.LoadPlugin(className, null, file, linkName); - var field = type.GetFieldEx("Instance"); - if (field == null) return Activator.CreateInstance(type) as DbProviderFactory; + // 如果还没有,就写异常 + if (!File.Exists(file)) throw new FileNotFoundException("缺少文件" + file + "!", file); + } + if (type == null) return null; - return Reflect.GetValue(null, field) as DbProviderFactory; + var asm = type.Assembly; + if (DAL.Debug) DAL.WriteLog("{2}驱动{0} 版本v{1}", asm.Location, asm.GetName().Version, name ?? className.TrimEnd("Client", "Factory")); + + var field = type.GetFieldEx("Instance"); + if (field == null) return Activator.CreateInstance(type) as DbProviderFactory; + + return Reflect.GetValue(null, field) as DbProviderFactory; + } + catch + { + if (ignoreError) return null; + + throw; + } } [DllImport("kernel32.dll")] diff --git a/XCode/DataAccessLayer/Database/MySql.cs b/XCode/DataAccessLayer/Database/MySql.cs index f14a336617..4f13cc8796 100644 --- a/XCode/DataAccessLayer/Database/MySql.cs +++ b/XCode/DataAccessLayer/Database/MySql.cs @@ -27,7 +27,7 @@ public override DbProviderFactory Factory //_Factory = GetProviderFactory("NewLife.MySql.dll", "NewLife.MySql.MySqlClientFactory") ?? // GetProviderFactory("MySql.Data.dll", "MySql.Data.MySqlClient.MySqlClientFactory"); // MewLife.MySql 在开发过程中,数据驱动下载站点没有它的包,暂时不支持下载 - _Factory = GetProviderFactory(null, "NewLife.MySql.MySqlClientFactory") ?? + _Factory = GetProviderFactory(null, "NewLife.MySql.MySqlClientFactory", true) ?? GetProviderFactory("MySql.Data.dll", "MySql.Data.MySqlClient.MySqlClientFactory"); } } @@ -382,7 +382,7 @@ protected override Boolean DatabaseExist(String databaseName) public override String CreateDatabaseSQL(String dbname, String file) { - return base.CreateDatabaseSQL(dbname, file)+ " DEFAULT CHARACTER SET utf8mb4"; + return base.CreateDatabaseSQL(dbname, file) + " DEFAULT CHARACTER SET utf8mb4"; } public override String DropDatabaseSQL(String dbname) => String.Format("Drop Database If Exists {0}", FormatName(dbname)); diff --git a/XCode/DataAccessLayer/Database/SQLite.cs b/XCode/DataAccessLayer/Database/SQLite.cs index f83afe34f0..beea98213f 100644 --- a/XCode/DataAccessLayer/Database/SQLite.cs +++ b/XCode/DataAccessLayer/Database/SQLite.cs @@ -35,7 +35,11 @@ public override DbProviderFactory Factory _Factory = GetProviderFactory("Mono.Data.Sqlite.dll", "Mono.Data.Sqlite.SqliteFactory"); else { - _Factory = GetProviderFactory(null, "Microsoft.Data.Sqlite.SqliteFactory"); +#if __CORE__ + _Factory = GetProviderFactory("Microsoft.Data.Sqlite.dll", "Microsoft.Data.Sqlite.SqliteFactory", true); +#else + //_Factory = GetProviderFactory(null, "Microsoft.Data.Sqlite.SqliteFactory"); +#endif if (_Factory == null) _Factory = GetProviderFactory("System.Data.SQLite.dll", "System.Data.SQLite.SQLiteFactory"); } }