Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add data annotations to a class generated by entity framework

I have the following class generated by entity framework:

public partial class ItemRequest {     public int RequestId { get; set; }     //... 

I would like to make this a required field

[Required] public int RequestId { get;set; } 

However, because this is generated code this will get wiped out. I can't imagine a way to create a partial class because the property is defined by the generated partial class. How can I define the constraint in a safe way?

like image 626
P.Brian.Mackey Avatar asked May 24 '13 13:05

P.Brian.Mackey


People also ask

What is data annotations in Entity Framework?

Advertisements. DataAnnotations is used to configure the classes which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of .

Does EF core support data annotation attributes?

Configuration enables you to override EF Core's default behaviour. Configuration can be applied in two ways, using the Fluent API, and through DataAnnotation attributes. Attributes are a kind of tag that you can place on a class or property to specify metadata about that class or property.

What is data annotation in C #?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.


2 Answers

The generated class ItemRequest will always be a partial class. This allows you to write a second partial class which is marked with the necessary data annotations. In your case the partial class ItemRequest would look like this:

using System.ComponentModel; using System.ComponentModel.DataAnnotations;  //make sure the namespace is equal to the other partial class ItemRequest namespace MvcApplication1.Models  {     [MetadataType(typeof(ItemRequestMetaData))]     public partial class ItemRequest     {     }      public class ItemRequestMetaData     {         [Required]         public int RequestId {get;set;}          //...     } } 
like image 125
MUG4N Avatar answered Sep 29 '22 02:09

MUG4N


As MUG4N answered you can use partial classes but will be better use interfaces instead. In this case you will have compilation errors if EF model doesn't correspond to validation model. So you can modify your EF models without fear that validation rules are outdated.

using System.Collections.Generic; using System.ComponentModel.DataAnnotations;  namespace YourApplication.Models {     public interface IEntityMetadata     {         [Required]         Int32 Id { get; set; }     }      [MetadataType(typeof(IEntityMetadata))]     public partial class Entity : IEntityMetadata     {         /* Id property has already existed in the mapped class */     } } 

P.S. If you are using project type which is differ from ASP.NET MVC (when you perform manual data validation) don't forget to register your validators

/* Global.asax or similar */  TypeDescriptor.AddProviderTransparent(     new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Entity), typeof(IEntityMetadata)), typeof(Entity)); 
like image 39
dimonser Avatar answered Sep 29 '22 01:09

dimonser