public ActionResult Auto(AutoUI model, FormCollection form) { #if DEBUG model.Tables = form["tables"]; model.Methods = form["Methods"]; var ri = new AutoHelpBiz().Create(model); return(Json(ri)); #endif return(Content("运行环境不正常")); }
// GET: Test/Test public ActionResult Index() { #if DEBUG DataTable tab = new AutoHelpBiz().GetTable(); ViewBag.dt = tab; AutoUI model = new AutoUI(); return(View(model)); #endif return(RedirectToAction("Index", "Home")); }
/// <summary> /// 自定生成数据从文件 /// </summary> /// <param name="tableName">表名称</param> /// <returns></returns> public ResultInfo Create(AutoUI model) { ResultInfo ri = new ResultInfo(); if (string.IsNullOrWhiteSpace(model.Tables)) { ri.Msg = "请选择表"; return(ri); } if (!model.IsBiz && !model.IsDataAccess && !model.IsEntity && !model.IsControl) { ri.Msg = "请选择生成数据"; return(ri); } if (string.IsNullOrWhiteSpace(model.Methods) && (model.IsDataAccess || model.IsBiz)) { ri.Msg = "请选择生成方法"; return(ri); } DataTable dt = GetTable(); if (dt == null || dt.Rows.Count == 0) { ri.Msg = "数据库中没有创建表"; return(ri); } List <string> listTable = (from d in dt.AsEnumerable() select d.Field <string>("Name")).ToList <string>(); StringBuilder errInfo = new StringBuilder(); string[] method = null; if (!string.IsNullOrWhiteSpace(model.Methods)) { method = model.Methods.Split(',');//需要生成的方法 } foreach (string tableName in model.Tables.Split(',')) { var queryTable = listTable.Contains(tableName); if (!queryTable) { errInfo.AppendLine("表名(" + tableName + ")不存在"); continue; } DataTable field = GetField(tableName); if (field == null || field.Rows.Count == 0) { errInfo.AppendLine("表(" + tableName + ")没有字段"); continue; } DataTable pk = GetPk(tableName); if (pk == null || pk.Rows.Count == 0) { errInfo.AppendLine("表(" + tableName + ")没有主键"); continue; } DataTable tableContent = ah.GetTableDesc(tableName); string Conents = string.Empty;//表说明 if (tableContent != null && tableContent.Rows.Count > 0) { Conents = tableContent.Rows[0]["contents"].ToString(); } DataTable desc = GetDescriptions(tableName); #region 是否生成实体层 string entityPath = GetPath(System.AppDomain.CurrentDomain.BaseDirectory, "Auto\\Temp"); string entityFile = tableName + ".cs";//实体层文件名称 if (model.IsEntity) { bool bolModels = CreateEntity(tableName, field, desc, entityFile, entityPath, Conents); errInfo.AppendLine(bolModels ? tableName + "实体层生成成功" : tableName + "实体层生成错误"); } #endregion #region 是否生成数据层 if (model.IsDataAccess) { string dataPath = GetPath(System.AppDomain.CurrentDomain.BaseDirectory, "Auto\\Temp"); string dalFileName = "Auto_" + tableName + "DAL.cs"; bool bolDataAceess = CreateDataAccess(tableName, pk.Rows[0]["column_name"].ToString(), field, dataPath, dalFileName, method, Conents); errInfo.AppendLine(bolDataAceess ? tableName + "数据层生成成功" : tableName + "数据层生成错误"); } #endregion #region 是否生成业务层 if (model.IsBiz) { string bizPath = GetPath(System.AppDomain.CurrentDomain.BaseDirectory, "Auto\\Temp"); //保存路径 string bizFile = "Auto_" + tableName + "BLL.cs"; //业务层文件名称 bool bolBusiness = CreateBiz(pk.Rows[0]["column_name"].ToString(), tableName, bizFile, bizPath, method, Conents); errInfo.AppendLine(bolBusiness ? tableName + "业务层生成成功" : tableName + "业务层生成错误"); } #endregion #region 是否生成控制器 if (model.IsControl) { string webPath = System.AppDomain.CurrentDomain.BaseDirectory + "Controllers\\"; string webFile = tableName + "Controller.cs";//控制器文件名称 bool bolWeb = CreateControl(tableName, webPath, webFile); errInfo.AppendLine(bolWeb ? tableName + "控制器生成成功" : tableName + "控制器生成错误"); } #endregion } ri.Msg = errInfo.ToString(); return(ri); }