Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

partial classes + DataAnnotations

I've got a class that was generated for me by the Entity Framework:

Models/EF.tt/Product.cs

public partial class X
{
  public int Name { get; set; }
  ...
}

I don't want to modify it because it's managed by the EF editor and it will wipe out my modifications any time I regenerate it, so I'm putting code into a separate file. Because the classes are declared as partial I can do useful things... what I haven't been able to figure out is how to use DataAnnotations for the properties.

Models/EF.custom.cs

public partial class X
{
  [Display(Name = "My Name")]
  public int Name { get; set; }
  ...
}

which fails... what is the proper way to do this?

like image 735
ekkis Avatar asked May 08 '11 02:05

ekkis


People also ask

Can we have partial classes in Javascript?

partial classes can work by convention. For example consider this convention. // file main function SomeObject() { for (var i = 0, ii = SomeObject. Partial.

What is DataAnnotations in C#?

DataAnnotations namespace includes the following validator attributes: Range – Enables you to validate whether the value of a property falls between a specified range of values. RegularExpression – Enables you to validate whether the value of a property matches a specified regular expression pattern.


1 Answers

Your going to want to use a metadatatype:

http://ryanhayes.net/blog/data-annotations-for-entity-framework-4-entities-as-an-mvc-model/

like image 170
Kevin LaBranche Avatar answered Oct 13 '22 19:10

Kevin LaBranche