Search code examples
asp.net-mvc-3ef-code-firstentity-framework-4.1

StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First


What is the difference in behavior of [MaxLength] and [StringLength] attributes?

As far as I can tell (with the exception that [MaxLength] can validate the maximum length of an array) these are identical and somewhat redundant?


Solution

  • MaxLength is used for the Entity Framework to decide how large to make a string value field when it creates the database.

    From MSDN:

    Specifies the maximum length of array or string data allowed in a property.

    StringLength is a data annotation that will be used for validation of user input.

    From MSDN:

    Specifies the minimum and maximum length of characters that are allowed in a data field.