private void SaveCodeTableToDb(CodeTableViewModel viewModel) { if (!string.IsNullOrEmpty(viewModel.TableName)) { viewModel.TableName = viewModel.TableName.Trim(); } if (!string.IsNullOrEmpty(viewModel.ClassName)) { viewModel.ClassName = viewModel.ClassName.Trim(); } base.Check(string.IsNullOrEmpty(viewModel.TableName) || string.IsNullOrEmpty(viewModel.ClassName), "表名或者实体类名必须填一个"); viewModel.ColumnInfoList = viewModel.ColumnInfoList .Where(it => !string.IsNullOrEmpty(it.ClassProperName) || !string.IsNullOrEmpty(it.DbColumnName)).ToList(); base.Check(viewModel.ColumnInfoList.Count == 0, "请配置实体属性"); var dbTable = mapper.Map <CodeTable>(viewModel); AutoFillTable(dbTable); var dbColumns = mapper.Map <List <CodeColumns> >(viewModel.ColumnInfoList); AutoFillColumns(dbColumns); if (viewModel.Id == null || viewModel.Id == 0) { CheckAddName(viewModel, CodeTableDb); var id = CodeTableDb.InsertReturnIdentity(dbTable); foreach (var item in dbColumns) { item.CodeTableId = id; item.DbColumnName = GetColumnName(item.DbColumnName); } CodeColumnsDb.InsertRange(dbColumns); } else { CheckUpdateName(viewModel, CodeTableDb); CodeTableDb.Update(dbTable); foreach (var item in dbColumns) { item.CodeTableId = dbTable.Id; item.DbColumnName = GetColumnName(item.DbColumnName); } var oldIds = CodeColumnsDb.GetList(it => it.CodeTableId == dbTable.Id).Select(it => it.Id).ToList(); var delIds = oldIds.Where(it => !dbColumns.Select(y => y.Id).Contains(it)).ToList(); CodeColumnsDb.DeleteByIds(delIds.Select(it => (object)it).ToArray()); var updateColumns = dbColumns.Where(it => it.Id > 0).ToList(); if (updateColumns.Count > 0) { CodeColumnsDb.UpdateRange(updateColumns); } var insertColumns = dbColumns.Where(it => it.Id == 0).ToList(); if (insertColumns.Count > 0) { CodeColumnsDb.InsertRange(insertColumns); } } }
public ActionResult <ApiResult <bool> > DeleteCodeTable([FromForm] string model) { var result = new ApiResult <bool>(); if (!string.IsNullOrEmpty(model)) { var list = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CodeTableViewModel> >(model); Db.Utilities.PageEach(list, 50, eitem => { var exp = Expressionable.Create <CodeTable>(); foreach (var item in eitem) { exp.Or(it => it.Id == item.Id); } CodeTableDb.Update(it => new CodeTable() { IsDeleted = true }, exp.ToExpression()); }); } result.IsSuccess = true; return(result); }