void InitTable(string ver) { try { Db.CodeFirst.InitTables( typeof(Datas), typeof(InDatas), typeof(Settings), typeof(Users), typeof(UsersBase), typeof(Counts), typeof(Admin), typeof(Logs), typeof(Station), typeof(Versions), typeof(InterfaceTime) ); if (!Db.Queryable <Counts>().Any()) { CountsDb.Insert(new Counts()); } if (!Db.Queryable <Admin>().Any()) { AdminDb.Insert(new Admin { UserName = "******", UserPassword = "******".ToPwd(), UserType = "管理员" }); } if (Db.Queryable <Versions>().Any()) { Db.Deleteable <Versions>().ExecuteCommand(); } VersionsDb.Insert(new Versions { Ver = ver }); } catch (Exception e) { MessageBox.Show(e.Message); Environment.Exit(0); } }
/// <summary> /// 同步数据库 /// </summary> public void DdAsyn() { var v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); try { Db.Ado.Open(); Db.Ado.Close(); //清理数据 DatasDb.Delete(p => p.CreateDate > DateTime.Now.AddMonths(-1)); InDatasDb.Delete(p => p.CreateDate > DateTime.Now.AddMonths(-1)); InterfaceTimeDb.Delete(p => p.CreateTime > DateTime.Now.AddMonths(-1)); //判断版本 if (!Db.DbMaintenance.GetTableInfoList().Any(p => p.Name == "Versions")) { InitTable(v); } else { if (!VersionsDb.IsAny(p => p.Ver == v)) { InitTable(v); } } } catch { InitTable(v); } finally { stopwatch.Stop(); #if DEBUG LogDb.Log.Info($"DB初始化执行耗时:{stopwatch.Elapsed.TotalMilliseconds}ms"); #endif } }