protected override void InitData() { base.InitData(); // InitData一般用于当数据表没有数据时添加一些默认数据,该实体类的任何第一次数据库操作都会触发该方法,默认异步调用 // Meta.Count是快速取得表记录数 if (Meta.Count > 0) { return; } // 需要注意的是,如果该方法调用了其它实体类的首次数据库操作,目标实体类的数据初始化将会在同一个线程完成 if (XTrace.Debug) { XTrace.WriteLine("开始初始化{0}[{1}]数据……", typeof(Info).Name, Meta.Table.DataTable.DisplayName); } // 遍历分类 NewLife.CMX.Category.Meta.Session.WaitForInitData(); var sb = new StringBuilder(); for (var i = 0; i < 20; i++) { sb.AppendLine("新生命开发团队,学无先后达者为师<br>"); } var txt = sb.ToString(); foreach (var item in NewLife.CMX.Category.FindAllWithCache()) { var entity = new Info() { ModelID = item.ModelID, CategoryID = item.ID, Title = "{0}信息".F(item.Name), ContentText = txt, }; // 顶级分类的信息,设置编码 if (item.ParentID == 0) { entity.Code = PinYin.GetFirst(entity.Title); } entity.Insert(); } if (XTrace.Debug) { XTrace.WriteLine("完成初始化{0}[{1}]数据!", typeof(Info).Name, Meta.Table.DataTable.DisplayName); } }
public async Task <IHttpActionResult> Update([FromBody] CompanyModel company) { return(await ResultFactory.Create(ModelState, async arg => { var result = await companyService.UpdateAsync(new CompanyDto { Name = company.Name, Pinyin = PinYin.GetFirst(company.Name).ToUpper(), Sort = 0, Status = 1, UpdateTime = DateTime.Now }) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, company, "success", "请检查请求参数")); }
private void AddEntity(string serialnum, string name, string parent, string description) { var entity = new FacilityType(); entity.Serialnum = serialnum; entity.Name = name; entity.ParentSerialnum = parent; entity.PhotoUrl = "FacilityType/" + PinYin.GetFirst(name) + ".png"; entity.Css = "xzss-icon xzss-icon-" + PinYin.GetFirst(name); entity.Introduce = description; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.UpdateSysUserSerialnum = "admin"; entity.Remark = "systype"; Repository.Add(entity); }
/// <summary>验证数据,通过抛出异常的方式提示验证失败。</summary> /// <param name="isNew"></param> public override void Valid(Boolean isNew) { // 如果没有脏数据,则不需要进行任何处理 if (!HasDirty) { return; } // 这里验证参数范围,建议抛出参数异常,指定参数名,前端用户界面可以捕获参数异常并聚焦到对应的参数输入框 if (Name.IsNullOrEmpty()) { throw new ArgumentNullException(_.Name, _.Name.DisplayName + "无效!"); } if (Model == null) { throw new ArgumentNullException(__.ModelID, _.ModelID.DisplayName + "无效!"); } // 建议先调用基类方法,基类方法会对唯一索引的数据进行验证 base.Valid(isNew); if (Dirtys[__.Code]) { // 已设置代码,检查是否唯一 if (!Code.IsNullOrEmpty()) { CheckExist(__.Code); } } else if (isNew) { // 未设置代码,自动生成 var code = PinYin.GetFirst(Name); Code = code; for (var i = 2; i < 100; i++) { if (!Exist(__.Code)) { break; } Code = code + i; } } }
/// <summary>验证数据,通过抛出异常的方式提示验证失败。</summary> /// <param name="isNew">是否插入</param> public override void Valid(Boolean isNew) { // 如果没有脏数据,则不需要进行任何处理 if (!HasDirty) { return; } // 这里验证参数范围,建议抛出参数异常,指定参数名,前端用户界面可以捕获参数异常并聚焦到对应的参数输入框 if (Name.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Name), "名称不能为空!"); } if (Code.IsNullOrEmpty()) { Code = PinYin.GetFirst(Name); } }
/// <summary> /// 添加设施类型 /// </summary> /// <param name="serialnum"></param> /// <param name="name"></param> /// <param name="parent"></param> /// <param name="description"></param> /// <returns></returns> private FacilityType AddEntity(string serialnum, string name, string parent, string description) { var entity = new FacilityType(); entity.Serialnum = serialnum; entity.Name = name; entity.ParentSerialnum = parent; entity.PhotoUrl = "FacilityType/" + PinYin.GetFirst(name) + ".png"; entity.Css = "xzss-icon xzss-icon-" + PinYin.GetFirst(name); entity.Introduce = description; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.UpdateSysUserSerialnum = "admin"; entity.Sort = 0; entity.Remark = "systype"; entity.IsSystem = true; FacilityTypeRep.Add(entity); FacilityTypeRep.Commit(); return(entity); }
/// <summary> /// 添加设备类型 /// </summary> /// <param name="serialnum">设施类型编码</param> /// <param name="name">名称</param> /// <param name="parent">上街</param> /// <param name="description">描述</param> /// <param name="url">url</param> private void AddEntity(string serialnum, string name, string parent, string description, string url = "") { var entity = new DeviceType(); entity.Serialnum = serialnum; entity.Name = name; entity.ParentSerialnum = parent; if (url.IsNullOrWhiteSpace()) { entity.PhotoUrl = "DeviceType/" + PinYin.GetFirst(name) + ".png"; } else { entity.PhotoUrl = "DeviceType/" + url + ".png"; } entity.Introduce = description; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.UpdateSysUserSerialnum = "admin"; entity.Remark = "systype"; entity.ValueCount = 0; entity.Sort = 0; Repository.Add(entity); }