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
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
- FieldError: Cannot resolve keyword 'id' into field in Django project
Related Article