asp.net mvc常用的資料註解和驗證以及entity framework資料對應

來源:互聯網
上載者:User

標籤:style   blog   color   資料   io   art   re   c   

終於有時間整理一下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 SnsModel11 {12     [Table("AdminInfo")]13     public partial class AdminInfo14     {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  
誠邀各路高手、初學者加入。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.