views:

282

answers:

2

Using aspnet mvc2 with the model auto-generated by entity framework:

Is it possible to tell entity framework to auto-annotate all fields? eg:

If database field says not null then add [Required] If DB field is a nvarchar(x) then add [StringLength(x)]

And so on?

What if the field name contains the string "email" eg CustomerEmail - can I get EF to auto-annotate that with an appropriate annotation ([Regex()] maybe)

As I understand it, if the model fields are annotated, and I use both Html.ValidationMessageFor() and use if (ModelState.IsValid) in my controller, then that is all I need to do to have basic clientside input validation working?

Thanks

+1  A: 

I am not aware of any automatically generated attributes.

As I understand it, if the model fields are annotated, and I use both Html.ValidationMessageFor() and use if (ModelState.IsValid) in my controller, then that is all I need to do to have basic clientside input validation working?

That is correct. http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html

Raj Kaimal
Its seems like it would be such a common requirement. Eg The field in the database is nvarchar(20), so you would expect to be able to somehow be able to get an automatic annotation of [StringLength(20)]. To have to manually type the annontation for each and every field would be very wrong.
JK
I saw your new blog post http://weblogs.asp.net/rajbk/archive/2010/05/04/a-basic-t4-template-for-generating-model-metadata-in-asp-net-mvc2.aspx solving this problem .. thanks a lot and +1 :)
JK
JK, the post is an early attempt. For example, you cannot make any changes to the auto generated fields. You could copy the generated class into another class but you have to do the extra work of making sure your classes are in sync.
Raj Kaimal
A: 

in jQuery, you can run through every object in a page. If it's client side validation you're looking for, you can use jQuery's field validation.

Martin Ongtangco
With 50+ tables and many hundreds of fields, any solution must be automatic. It would be wrong to manually type any code when the database already contains the metadata describing if the field is required, the field length and so on.
JK
you can separate these fields from one another by applying classes to the objects. for say, you search every input field there is, then locate if this field has, let's say, a "chumbawamba" class name in it.
Martin Ongtangco