Common data annotations and validation for ASP. NET MVC and Entity Framework Data mapping

Source: Internet
Author: User

Finally have time to sort out the source of ASP. NET MVC and Entity Framework.

Talk less, step into the subject:

The following is the model layer of the Administrator information table, is also commonly used by everyone, see the following code should not be unfamiliar,

In this model we use the data annotation and validation of ASP. NET MVC, the Entity Framework maps the database

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.ComponentModel.DataAnnotations.Schema;6 usingSystem.ComponentModel.DataAnnotations;7 usingSYSTEM.WEB.MVC;8 usingSystem.ComponentModel;9 Ten namespaceSnsmodel One { A[Table ("Admininfo")] -      Public Partial classAdmininfo -     { the [Key] -[Display (Name ="Number:")] -[Column ("Id")] -          Public intId {Get;Set; } +  -[Required (errormessage ="* cannot be empty!")] +[RegularExpression (@"^[\u4e00-\u9fa5\uf900-\ufa2d\w\.\s]{6,18}$", errormessage ="*6-18 bit Pinyin or numbers")] A[Column (TypeName ="nvarchar")] at[MaxLength ( -)] -[Display (Name ="User name:")] -         ///[Remote ("Checkusername", "Account")] -          Public stringUserName {Get;Set; } -  -[Required (errormessage ="* cannot be empty!")] in[RegularExpression (@"^[\u4e00-\u9fa5\uf900-\ufa2d\w\.\s]{6,18}$", errormessage ="*6-18 bit Pinyin or numbers")] -[Column (TypeName ="nvarchar")] to[MaxLength ( -)] +[MinLength (6)] -[Display (Name ="Password:")] the [DataType (Datatype.password)] *          Public stringPassWord {Get;Set; } $ Panax Notoginseng[Column (TypeName ="nvarchar")] -[Display (Name ="real name:")] the[MaxLength ( -)] +          Public stringTruename {Get;Set; } A  the[Display (Name ="is available:")] +          Public BOOL? isuseful {Get;Set; } -  $[ReadOnly (true)] $[Display (Name ="creation Time:")] -          PublicDateTime Creattime {Get;Set; } -  the[Display (Name ="level")] -          Public int? Orders {Get;Set; }Wuyi  the [notmapped] -[Required (errormessage ="* cannot be empty!")] Wu[RegularExpression (@"^[\u4e00-\u9fa5\uf900-\ufa2d\w\.\s]{6,18}$", errormessage ="*6-18 bit Pinyin or numbers")] -[Display (Name ="New Password")] About [DataType (Datatype.password)] $          Public Virtual stringPassWord1 {Get;Set; } -  - [notmapped] -[Compare ("PassWord1", errormessage ="The new password and confirmation password are inconsistent!")] A[Required (errormessage ="* cannot be empty!")] +[RegularExpression (@"^[\u4e00-\u9fa5\uf900-\ufa2d\w\.\s]{6,18}$", errormessage ="*6-18 bit Pinyin or numbers")] the[Display (Name ="Confirm Password")] - [DataType (Datatype.password)] $          Public Virtual stringPassWord2 {Get;Set; } the     } the}

The code above is very simple, there is no need to explain too much.

Recently, when discussing ASP. NET Mvc+ef with some group of friends, it is very common that many people abandon the MVC data annotation and validation, why? Some people may not be very good,

Others because of the use of other jquery plug-ins (such as Easyui), which led to a consequence, the foreground verified that the background is not verified. This poses a great danger to the project, and flashy projects are of no value.

Originally in the study of one months after the Easyui found that Easyui in fact, and MVC is also very good compatibility. Of course, at the very beginning, I found that the 2 jquery is really a conflict, after a careful study of the integration, everything is OK.

Originally also rookie level, such as speech and code what is wrong, but also hope that Daniel guidance, younger brother grateful.

The following are QQ group ads:

This group provides ASP. NET Mvc,ef,linq,web API technical support, do not care about people, care about human spermatogonial.
ASP. NET MVC Group 171560784
Invite all the experts, beginners to join.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.