12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Stringの長さを指定するMaxLength/StringLength属性はどう違うのか

Posted at

DBに入れたタイミングでチェックする(MaxLength)か、ユーザが画面から入力した際にチェックする(StringLength)か、の違い。

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.

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

最小入力値のチェックはMaxLength属性はMinLength属性にて、StringLength属性はMinimumLengthプロパティで設定。


        [MaxLength(100)]
        [MinLength(5)]
        [StringLength(100,MinimumLength =5)]
        public string updateByName { get; set; }

ErrorMessageの変数は以下の通り。

  • {0} = Property Name
  • {1} = Max Length
  • {2} = Min Length

StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First - Stack Overflow
http://stackoverflow.com/questions/5717033/stringlength-vs-maxlength-attributes-asp-net-mvc-with-entity-framework-ef-code-f

12
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?