We can use metadata to add functionality to entities by adding properties in a metadata class with attributes that we can affect the behavior of entities implemented with partial class called metadata classes.
When you needed to update the entities model but you do not want to lose the annotations when the entity class is regenerated. Metadata class allows adding functionality such as validation and child entity child entity compositon.
Entity generated for post Table:
public partial class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
To add metadata class manually:
[Table("Post")]
[MetadataType(typeof(PostMetadata))]
public partial class Post
{
internal sealed class PostMetadata
{
[AllowHtml]
public string Title { get; set; }
[AllowHtml]
[StringLength(500, ErrorMessage = "Description can acceptmaximum 500 characters.")]
public string Description { get; set; }
[AllowHtml]
[Required]
}
}
Post your comments / questions
Recent Article
- How to Encode & Decode using Base64 in Python?
- Unspecified error-The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support.
- How to generate a Captcha verification code image with Python?
- How to show an image using path python PIL?
- How to remove Background from the images using Python?
- Python generate QR code image
- Insert excel file data into MySQL database in Python
- ImportError: Missing optional dependency 'xlrd' | How tot Convert xls to xlsx | Python
Related Article