終於有時間整理一下asp.net mvc 和 entity framework 方面的素材了。
閑話少說,步入正題:
下面是model層的管理員信息表,也是大伙比較常用到的,看看下面的代碼大伙應該不會陌生,
在此Model上我們用到了asp.net mvc的數據注解和驗證,entity framework對數據庫的映射
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ComponentModel.DataAnnotations.Schema; 6 using System.ComponentModel.DataAnnotations; 7 using System.Web.Mvc; 8 using System.ComponentModel; 9 10 namespace SnsModel 11 { 12 [Table("AdminInfo")] 13 public partial class AdminInfo 14 { 15 [Key] 16 [Display(Name = "編號:")] 17 [Column("Id")] 18 public int Id { get; set; } 19 20 [Required(ErrorMessage = "*不能為空!")] 21 [RegularExpression(@"^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$", ErrorMessage = "*6-18位拼音或數字")] 22 [Column(TypeName = "nvarchar")] 23 [MaxLength(50)] 24 [Display(Name = "用戶名:")] 25 ///[Remote("CheckUserName","Account")] 26 public string UserName { get; set; } 27 28 [Required(ErrorMessage = "*不能為空!")] 29 [RegularExpression(@"^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$", ErrorMessage = "*6-18位拼音或數字")] 30 [Column(TypeName = "nvarchar")] 31 [MaxLength(50)] 32 [MinLength(6)] 33 [Display(Name = "密碼:")] 34 [DataType(DataType.Password)] 35 public string PassWord { get; set; } 36 37 [Column(TypeName = "nvarchar")] 38 [Display(Name = "真實姓名:")] 39 [MaxLength(20)] 40 public string TrueName { get; set; } 41 42 [Display(Name = "是否可用:")] 43 public bool? IsUseFul { get; set; } 44 45 [ReadOnly(true)] 46 [Display(Name = "創建時間:")] 47 public DateTime CreatTime { get; set; } 48 49 [Display(Name = "等級")] 50 public int? Orders { get; set; } 51 52 [NotMapped] 53 [Required(ErrorMessage = "*不能為空!")] 54 [RegularExpression(@"^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$", ErrorMessage = "*6-18位拼音或數字")] 55 [Display(Name = "新密碼")] 56 [DataType(DataType.Password)] 57 public virtual string PassWord1 { get; set; } 58 59 [NotMapped] 60 [Compare("PassWord1", ErrorMessage = "新密碼和確認密碼不一致!")] 61 [Required(ErrorMessage = "*不能為空!")] 62 [RegularExpression(@"^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$", ErrorMessage = "*6-18位拼音或數字")] 63 [Display(Name = "確認密碼")] 64 [DataType(DataType.Password)] 65 public virtual string PassWord2 { get; set; } 66 } 67 }
上面的代碼很簡單,在此沒有必要過多解釋什么。
最近在跟一些群友探討asp.net mvc+ef的時候,很常見這樣的問題,很多人舍棄了mvc的數據注解和驗證,為什么呢?一些人可能是不太會,
另一些人是因為用了其他的jquery插件(如easyui),如此導致了一個后果,前台驗證了后台沒有驗證。這將給項目帶來很大的危險性,華而不實的項目是沒有價值的。
本來在研究了一個月的easyui之后發現,easyui其實跟mvc也是能很好的兼容的。當然最開始的時候,我發現2者的jquery確實是沖突,細細研究整合之后,一切都是很OK的。
本來也是菜鳥級別,如言論和代碼有什么錯誤之處,還望大牛們指點,小弟感激不盡。
以下為QQ群廣告:
本群提供ASP.NET MVC,EF,LINQ,WEB API技術支持,不在乎人多,在乎人精。
ASP.NET MVC群 171560784
誠邀各路高手、初學者加入。