//[Priviledge(Name = "首页", IsMenu = true, IsEntry = true, Position = 1)] public ActionResult Index() { using (var session = new SessionFactory().OpenSession()) { const string accountNavigationSql = "SELECT * FROM `navigations` WHERE `type` < 4 AND ((`auth_code` = 0) OR (`id` IN ( SELECT `navigation_id` FROM `navigation_priviledges` WHERE (`flag` = 1 AND `owner_id` IN (SELECT `role_id` FROM `account_role_refs` WHERE `account_id` IN ( SELECT `id` FROM `accounts` WHERE `name` = '{0}'))) OR (`flag` = 2 AND `owner_id` IN ( SELECT `id` FROM `accounts` WHERE `name` = '{0}'))))) ORDER BY `order_id` asc"; var accountNavs = session.FindBySql<Navigation>(string.Format(accountNavigationSql, CurrentAccountNo)); Session.Add(Const.AccountMenuItems, accountNavs); const string rolePinNavigationSql = "SELECT n.* FROM `account_navigation_refs` r LEFT JOIN `navigations` n ON r.`navigation_id` = n.`id` WHERE r.`type` = 1 AND r.`owner_id` IN (SELECT `role_id` FROM `account_role_refs` WHERE `account_id` IN ( SELECT `id` FROM `accounts` WHERE `name` = '{0}')) ORDER BY r.`order_id` ASC"; var pinRoleNavs = session.FindBySql<Navigation>(string.Format(rolePinNavigationSql, CurrentAccountNo)); Session.Add(Const.RolePinnedTask, pinRoleNavs); const string accountPinNavigationSql = "SELECT n.* FROM `account_navigation_refs` r LEFT JOIN `navigations` n ON r.`navigation_id` = n.`id` WHERE r.`type` = 2 AND r.`owner_id` IN ( SELECT `id` FROM `accounts` WHERE `name` = '{0}') ORDER BY r.`order_id` ASC"; var pinAccountNavs = session.FindBySql<Navigation>(string.Format(accountPinNavigationSql, CurrentAccountNo)); Session.Add(Const.AccountPinnedTask, pinAccountNavs); var model = session.Load<Account>(m => m.Name.Equals(CurrentAccountNo)); return View(model); } }
public static string GetModelCodeFormDb(string tableName) { using (var session = new SessionFactory().OpenSession()) { var model = session.FindBySql<DbTable>("show full fields from " + tableName); if (model == null || model.Count == 0) { // table不存在 return null; } var tb = new StringBuffer(); foreach (var col in model) { tb += Environment.NewLine; if (col.Null.Equals("NO")) { tb += string.Format("[Required]{0}", Environment.NewLine); } if (col.Key.Equals("PRI")) { tb += "[PrimaryKey]" + Environment.NewLine; } if (!string.IsNullOrEmpty(col.Comment)) { tb += string.Format("[DisplayName(\"{0}\")]{1}", col.Comment, Environment.NewLine); } var type = col.Type; if (type.Contains("(")) { var temp = type.Split('('); type = temp[0]; var length = temp[1].Substring(0, temp[1].LastIndexOf(")", StringComparison.Ordinal)); if (type.Equals("text") || type.Equals("varchar")) tb += string.Format("[StringLength({0})]{1}", length, Environment.NewLine); } var name = col.Field.Pascalize(); var mappingDic = new Dictionary<string, string> { {"tinyint", "bool"}, {"int", "int"}, {"bigint", "long"}, {"varchar", "string"}, {"text", "string"}, {"datetime", "DateTime"}, {"decimal", "Decimal"} }; //未匹配的默认为string var typeStr = mappingDic[type] ?? "string"; tb += string.Format("public {0}{1}{2}{3}{4}", typeStr, (col.Null.Equals("YES") && !typeStr.Equals("string") ? "? " : " "), name, "{get; set;}", Environment.NewLine); } return tb.ToString(); } }