public static bool UpdateDB() {//一次性打包更新 try { var ScoreMapCach = ExtendMethord.GetScore().scoreMap; if (ScoreMapCach["ModifyFlag"] == 0) { return(false); } var temp = ScoreMapCach.Where(c => true); DbContext db = DBContextFactory.CreateDbContext(); //@刘剑_1989: 更新是这样的, //T existing = Context.Set<T>().Find //如果 existing == null, Context.Set<T>().Add(item); //否则, 将 item 的值赋给 existing(不包括主键的值), //最后,Context.SaveChanges foreach (var item_ in temp) { var tempModels = db.Set <ExtensionAgents>().Where <ExtensionAgents>(c => c.GUID == item_.Key); var Scores = tempModels.Select(c => c.ExtensionScore).FirstOrDefault() + item_.Value; foreach (var item in tempModels) { ExtensionAgents EA = db.Set <ExtensionAgents>().Find(item.ID); if (EA != null) { EA.DelFlag = item.DelFlag; EA.ExtensionScore = Scores; EA.ID = item.ID; EA.GUID = item.GUID; EA.LName = item.LName; EA.Remark = item.Remark; EA.RouteStatisticsLinks_ID = item.RouteStatisticsLinks_ID; EA.ModifiedOn = DateTime.Now; EA.Sort = item.Sort; EA.ExtensionUrl = item.ExtensionUrl; EA.SubTime = item.SubTime; EA.UrlName = item.UrlName; } else { return(false); } } } db.SaveChanges(); List <string> list = new List <string>(); list.AddRange(ScoreMapCach.Keys); foreach (var item in list) { ScoreMapCach[item] = 0; //计数以后清空 } ScoreMapCach["ModifyFlag"] = 0; // 修改位置为0 } catch (Exception e) { return(false); } return(true); }
public UserDetailsController() { BaseDal _dal = new BaseDal(DBContextFactory.CreateDbContext()); dal = _dal; runtimeModel = _dal.GetRuntimeModelType(GetJsonDatas.GetJson(), 0); }
public static bool CreatDefaultTableData() { //防止自动迁移 //System.Data.Entity.Database.SetInitializer<Yuruisoft.RS.Model.Yuruisoft_DBContext>(null); //System.Data.Entity.Database.SetInitializer<Yuruisoft.RS.Model.RuntimeDBContext>(null); //设置自动迁移 //System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<Yuruisoft.RS.Model.Yuruisoft_DBContext, Yuruisoft.RS.Model.Migrations.Configuration>()); //当自动迁移时,就drop //System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<Yuruisoft.RS.Model.RuntimeDBContext>()); //System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<Yuruisoft.RS.Model.Yuruisoft_DBContext>()); // false 异动,true 没有异动 //if (FirstDBcontext.Database.Exists()) //{ // var isChanged = DBContextFactory.CreateDbContext().Database.CompatibleWithModel(false); //} //以上为保留代码,后期动态类型会有有用 DbContext FirstDBcontext = DBContextFactory.CreateDbContext(); //CodeFirst首次创建数据库,首次创建DBContext,数据库不存在情况下 if (FirstDBcontext.Database.CreateIfNotExists()) { #region 读文件处理(含异常处理) string path = System.Web.HttpContext.Current.Server.MapPath("/App_Data/Config/Yuruisoft.RS.Config/DBInitialize.sql"); string strjson = null; try { strjson = System.IO.File.ReadAllText(path, Encoding.Default); if (ConfigurationManager.AppSettings["DBInitializePath"].ToString() == "") { // ConfigurationManager.AppSettings.Add("JsonConfigPath", strjson); 只读占用 XmlDocument webconfigDoc = new XmlDocument(); string filePath = System.AppDomain.CurrentDomain.BaseDirectory + @"/web.config"; //设置节的xml路径 string xPath = "/configuration/appSettings/add[@key='?']"; //加载web.config文件 webconfigDoc.Load(filePath); //找到要修改的节点 XmlNode passkey = webconfigDoc.SelectSingleNode(xPath.Replace("?", "DBInitializePath")); //设置节点的值 passkey.Attributes["value"].InnerText = path; //保存设置 webconfigDoc.Save(filePath); } } catch (Exception e) { if (e.GetType() == typeof(ArgumentException)) { try { strjson = System.IO.File.ReadAllText(ConfigurationManager.AppSettings["DBInitializePath"].ToString()); } catch (Exception ex) { throw ex; } } } #endregion FirstDBcontext.Database.ExecuteSqlCommandAsync(strjson, new SqlParameter("DateTime_now", DateTime.Now.ToString())); return(true); } return(false); }