Mvc+entity Framework database First, the generated model updates all of the model each time a table is updated

Source: Internet
Author: User

When using the entity Framework Database frist or model first, it is too realistic and unreasonable to directly add attribute to the Modle class, because the model class is automatically generated and will overwrite its own modifications when regenerated. A more reasonable approach is to use the partial class approach to achieve.

For example, there is a model class: Movie. Then we can add a local class file, the contents of the local class are as follows:

usingSystem.ComponentModel.DataAnnotations;namespacemovies.models{[Metadatatype (typeof(Moviemetadata))]  Public Partial classMovie {classMoviemetadata {[Required (errormessage="Titles are required")]             Public stringTitle {Get;Set; } [Required (ErrorMessage="The price is required.")] [Range (5, -, errormessage ="Movies cost between $ and $.")]             Public decimalPrice {Get;Set; } }    }}

This is done by adding the required validation attributes to the local class file.

Example 2:

[Metadatatype (typeof(Fileuploadermetadata))]//data validation with Fileuploadermetadata for Fileuploader     Public Partial classfileuploader:ibaseentity {#regionCustom attributes, which are entities extended by the data entity#endregion    }     Public classFileuploadermetadata {[Scaffoldcolumn (false)] [Display (Name="PRIMARY Key", Order =1)]             Public ObjectId {Get;Set; } [Scaffoldcolumn (true)] [Display (Name="name", Order =2)] [Stringlength ( $, errormessage ="length must not exceed")]             Public ObjectName {Get;Set; } [Scaffoldcolumn (true)] [Display (Name="Path", Order =3)] [Stringlength ( $, errormessage ="length must not exceed")]             Public ObjectPath {Get;Set; } }

Mvc+entity Framework database First, the generated model updates all of the model each time a table is updated

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.