/// <summary> /// 取得指定类型的 系统配置数值. /// </summary> /// <param name="configTypeCode"></param> /// <returns></returns> public List <SystemConfigValue> GetSystemConfigValueByType(string configTypeCode) { using (MySystemConfigContext context = new MySystemConfigContext()) { // 首先,取得类型. SystemConfigType systemConfigType = context.SystemConfigTypes.Find(configTypeCode); if (systemConfigType == null) { // 类型为空, 返回空白列表. return(new List <SystemConfigValue>()); } // 然后, 查询明细. var query = from data in context.SystemConfigValues where data.ConfigTypeCode == configTypeCode orderby data.ConfigCode select data; // 取得结果. List <SystemConfigValue> resultList = query.ToList(); // 配置的数据类型. Type configType = null; try { configType = Type.GetType(systemConfigType.ConfigClassName); } catch (Exception) { configType = null; } foreach (var item in resultList) { // Json 反序列化. if (configType != null) { // 指定了数据类型. item.ConfigValueObject = JsonConvert.DeserializeObject(item.ConfigValue, configType); } else { // 未指定数据类型. item.ConfigValueObject = JsonConvert.DeserializeObject(item.ConfigValue); } } return(resultList); } }
/// <summary> /// 取得配置类型 /// </summary> /// <param name="configTypeCode"></param> /// <returns></returns> public SystemConfigType GetSystemConfigType(string configTypeCode) { using (MySystemConfigContext context = new MySystemConfigContext()) { var result = context.SystemConfigTypes.Find(configTypeCode); return(result); } }
/// <summary> /// 取得全部的 系统配置类型. /// </summary> /// <returns></returns> public List <SystemConfigType> GetAllSystemConfigType() { using (MySystemConfigContext context = new MySystemConfigContext()) { var query = from data in context.SystemConfigTypes select data; return(query.ToList()); } }
/// <summary> /// 更新配置信息 /// </summary> /// <param name="configValue"></param> /// <param name="resultMessage"></param> /// <returns></returns> public bool UpdateSystemConfigValue(SystemConfigValue configValue, ref string resultMessage) { using (MySystemConfigContext context = new MySystemConfigContext()) { // 首先, 检查 ConfigTypeCode, ConfigCode 是否非空. if (String.IsNullOrEmpty(configValue.ConfigTypeCode)) { resultMessage = "配置类型代码不能为空."; return(false); } if (String.IsNullOrEmpty(configValue.ConfigCode)) { resultMessage = "配置代码不能为空."; return(false); } // 其次, 检查 ConfigTypeCode 是否存在. var configType = context.SystemConfigTypes.Find(configValue.ConfigTypeCode); if (configType == null) { resultMessage = String.Format("配置类型代码 {0} 不存在.", configValue.ConfigTypeCode); return(false); } // 用户数据, 以Json格式存储. if (configValue.ConfigValueObject != null) { configValue.ConfigValue = JsonConvert.SerializeObject(configValue.ConfigValueObject); } // 最后, 尝试查询数据, 判断是 插入, 还是更新. var dbConfigValue = context.SystemConfigValues.Find(configValue.ConfigTypeCode, configValue.ConfigCode); if (dbConfigValue == null) { // 插入处理. context.SystemConfigValues.Add(configValue); } else { // 更新数据. dbConfigValue.ConfigValue = configValue.ConfigValue; } context.SaveChanges(); return(true); } }
/// <summary> /// 取得指定类型的 系统配置属性. /// </summary> /// <param name="configTypeCode"></param> /// <returns></returns> public List <SystemConfigProperty> GetSystemConfigPropertyByType(string configTypeCode) { using (MySystemConfigContext context = new MySystemConfigContext()) { var query = from data in context.SystemConfigPropertys where data.ConfigTypeCode == configTypeCode orderby data.DisplayOrder select data; return(query.ToList()); } }
static void Main(string[] args) { // 当 Code First 与数据库结构不一致时 // 自动升级到最新的版本. Database.SetInitializer(new MigrateDatabaseToLatestVersion <MySystemConfigContext, MySystemConfig.Migrations.Configuration>()); using (MySystemConfigContext context = new MySystemConfigContext()) { var query = from data in context.SystemConfigTypes select data; foreach (var item in query) { Console.WriteLine(item.ConfigTypeName); } } Console.WriteLine("Finish!"); Console.ReadLine(); }
/// <summary> /// 取得系统配置数值 /// </summary> /// <param name="configTypeCode"></param> /// <param name="configCode"></param> /// <returns></returns> public SystemConfigValue GetSystemConfigValue(string configTypeCode, string configCode) { using (MySystemConfigContext context = new MySystemConfigContext()) { SystemConfigValue result = context.SystemConfigValues.Find(configTypeCode, configCode); if (result == null) { return(result); } // 配置的数据类型. Type configType = null; try { configType = Type.GetType(result.SystemConfigTypeData.ConfigClassName); } catch (Exception) { configType = null; } // Json 反序列化. if (configType != null) { // 指定了数据类型. result.ConfigValueObject = JsonConvert.DeserializeObject(result.ConfigValue, configType); } else { // 未指定数据类型. result.ConfigValueObject = JsonConvert.DeserializeObject(result.ConfigValue); } return(result); } }
// GET: SystemConfigValue public ActionResult Index(string id) { SystemConfigType typeData = this.systemConfigService.GetSystemConfigType(id); if (typeData == null) { return(HttpNotFound()); } ViewBag.SystemConfigType = typeData; List <SystemConfigValue> dataList = null; using (MySystemConfigContext context = new MySystemConfigContext()) { var query = from data in context.SystemConfigValues select data; if (!String.IsNullOrEmpty(id)) { query = query.Where(p => p.ConfigTypeCode == id); } // 查询数据. dataList = query.ToList(); } if (typeData.ConfigClassName == SystemConfigType.SimpleDictionary) { // 如果是简单 Key-Value。 // 还需要额外处理查询条件。 foreach (var data in dataList) { data.ConfigValueObject = JsonConvert.DeserializeObject(data.ConfigValue, Type.GetType(typeData.ConfigClassName)); } var dataQuery = from data in dataList select data; // 配置属性 List <SystemConfigProperty> scpList = this.systemConfigService.GetSystemConfigPropertyByType(id); foreach (var scp in scpList) { if (scp.IsSearchAble) { string queryString = Request[scp.PropertyName]; if (!String.IsNullOrEmpty(queryString)) { switch (scp.PropertyDataType) { case "System.Boolean": bool boolValue = Convert.ToBoolean(queryString); dataQuery = dataQuery.Where(p => p.ConfigValueObject[scp.PropertyName] == boolValue); break; case "System.String": dataQuery = dataQuery.Where(p => p.ConfigValueObject[scp.PropertyName].Contains(queryString)); break; case "System.Int32": case "System.Int64": long intValue = 0; if (Int64.TryParse(queryString, out intValue)) { dataQuery = dataQuery.Where(p => p.ConfigValueObject[scp.PropertyName] == intValue); } break; } } } } List <SystemConfigValue> resultList = dataQuery.ToList(); return(View(model: resultList)); } return(View(model: dataList)); }