4

I am considering using xVal for validation of Entity Framework classes in a MVC application. This involves writing metadata classes as explained in details by Graham O'Neale (http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework).

I am wondering if there's a way to auto generate such metadata classes using the metadata from the SQL database (for example: not null fields will have [Required] class attribute.

K.A.D.
  • 3,648
  • 3
  • 34
  • 35

3 Answers3

3

You could use Code Smith tool www.codesmithtools.com (there is a free version if I remember correctly)

Gregoire
  • 24,219
  • 6
  • 46
  • 73
1

I wrote an application that will read an existing database and then generate a Data Annotation Class for every table (excluding aspnet_* and VersionInfo).

http://pfsolutions-mi.com/Product/MetaDataClassGenerator

Frank Perez
  • 993
  • 8
  • 18
0

In EF 4, you can easily customize the code generation via a T4 template.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • This requires Visual Studio 2010. I only have Visual Studio 2008. – K.A.D. Feb 09 '10 at 18:01
  • There are T4 producers for VS 2008, too. But in your shoes I'd rather make a custom validation provider which understands EF attributes. Actually, I'd *really* rather use edit models. But that's not what you asked.... – Craig Stuntz Feb 09 '10 at 19:08
  • I dont mind changing the validation provider. Which validation provider which understands EF attributes? or how to use edit models? – K.A.D. Feb 10 '10 at 09:40
  • Validation providers: You'd have to write one. But it's not hard: http://bradwilson.typepad.com/blog/2010/01/why-you-dont-need-modelmetadataattributes.html Edit models: This just means you create a type for the sole purpose of binding your form, and project your entity onto that, instead of binding directly to the entity. – Craig Stuntz Feb 10 '10 at 13:36