WCF RIA Hosting :: WCF RIA Service adding extra Required attribute on generated classes

We upgraded a Silverlight 3 RIA application to Silveright 4 bits. Once we fixed all the errors, it appeared that RIA generated entities were “over-validating” themselves. We noticed that all the string entities that were marked not-nullable in database were giving validation errors saying “Field XYZ is required”. We looked through the attribute metadata classes and those field were not marked Required. However, the generated RIA classes had Required and StringLength attributes automatically applied on such fields. It looks RIA code generation is over-efficient and it automatically adds Required attributes for all non-nullable database fields. This doesn’t seem much problematic since we can easily write an implementation of the partial method OnCreated for our generated entities where we can assign default values to all fields. As a generic approach, we can write a reflective method that assign default values like this:

The above method needs to be called from the partial implementation of OnCreated for our generated data classes. However, things will still not work normally as there’s still a small point left: The Required attribute on string fields does not allow empty string. To tackle this, we need to decorate all our string fields in the metadata class (located in server project) with [Required(AllowEmptyString = true)]. That’s all

Comments are closed.



Cheap Reseller Hosting

Best Blog Hosting Provider

Best European ASP.NET 4.0 Hosting Provider