views:

310

answers:

2

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.

+1  A: 

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

Gregoire
A: 

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

Craig Stuntz
This requires Visual Studio 2010. I only have Visual Studio 2008.
Khalil Dahab
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
@Craig Stuntz: I dont mind changing the validation provider. Which validation provider which understands EF attributes? or how to use edit models?
Khalil Dahab
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