locked
Updating to EF 4.1 RC from CTP 5 - StringLength Validation? RRS feed

  • Question

  • Are there defaults for the StringLength Validation that have been added automatically?

    I don't have ANY [StringLength] attributes in my code, but my strings are sometimes failing validation because of their length. I can see that the model was generated with the Max Length setting of 128 (if i open the .edmx file generated from the model).

    Why is this being added and how can I change the default?

     

    Thursday, March 17, 2011 1:54 AM

Answers

  • Hi tlmii,

    What changed in EF 4.1 RC was how we perform the validation. Now some facets (MaxLength and Nullable) are validated by default.

    Currently the facet validation can't be turned off. You could hovewer turn off validation completely, refer to the "Customizing validation" section in http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx

    Also it isn't possible to modify the default string length. So the correct way to solve this for your scenario is to specify the StringLength attribute on all of the properties affected. We understand that this is tedious work and we'll try to improve this in the future.

    Thanks for sharing this,

    Andriy Svyryd

    • Marked as answer by tlmii Friday, March 18, 2011 12:41 PM
    Thursday, March 17, 2011 7:33 PM

All replies

  • Hi tlmii,

    Welcome!

    StringLength Class is defined in System.ComponentModel.DataAnnotations, so you should add reference(System.ComponentModel.DataAnnotation.dll) to your ClassLibary.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 17, 2011 3:40 AM
  • Hi Alan,

    Thanks for the response. Unfortunately I think you may have misunderstood my question. Let me clarify:

    1) I was running EF CTP 5 and compiling/executing my builds without issue. I was using NO StringLength attributes to specify things. They just got saved without issue.

    2) I upgraded to EF 4.1 RC.

    3) Without changing any of my code (except what it took to make it compile since there were NUMEROUS namespace/class name changes), I am now getting a validation error when saving some of my entities, saying that the string is too long (longer than 128).

    4) I can correct this by adding a StringLength attribute to those particular properties and increasing the value from the default of 128 to something else.

    However, I still have the following questions:

    A) What changed between CTP 5 and RC 1 that caused this validation to just start to fail now? Was validation previously turned off? Was the default string length just longer so i didn't run into it? Whatever the change is, why isn't this change documented somewhere?

    B) Is there a way to change the DEFAULT string length validation? It is clearly defaulting to 128 right now (since I'm not currently setting it anywhere). It would be useful to me to be able to change the default value rather than having to go around and add a StringLength attribute onto every one of my strings.

    C) Alternatively to B, is there a way to just turn off string length validation altogether?

    Thanks again for your assistance.

    Thursday, March 17, 2011 11:55 AM
  • Hi tlmii,

    What changed in EF 4.1 RC was how we perform the validation. Now some facets (MaxLength and Nullable) are validated by default.

    Currently the facet validation can't be turned off. You could hovewer turn off validation completely, refer to the "Customizing validation" section in http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx

    Also it isn't possible to modify the default string length. So the correct way to solve this for your scenario is to specify the StringLength attribute on all of the properties affected. We understand that this is tedious work and we'll try to improve this in the future.

    Thanks for sharing this,

    Andriy Svyryd

    • Marked as answer by tlmii Friday, March 18, 2011 12:41 PM
    Thursday, March 17, 2011 7:33 PM