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

前端 未结 8 1636
情歌与酒
情歌与酒 2020-12-04 07:47

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

As far as I can tell (with the exception that [MaxLe

相关标签:
8条回答
  • 2020-12-04 07:49

    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.

    0 讨论(0)
  • 2020-12-04 07:52

    Some quick but extremely useful additional information that I just learned from another post, but can't seem to find the documentation for (if anyone can share a link to it on MSDN that would be amazing):

    The validation messages associated with these attributes will actually replace placeholders associated with the attributes. For example:

    [MaxLength(100, "{0} can have a max of {1} characters")]
    public string Address { get; set; }
    

    Will output the following if it is over the character limit: "Address can have a max of 100 characters"

    The placeholders I am aware of are:

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

    Much thanks to bloudraak for initially pointing this out.

    0 讨论(0)
  • 2020-12-04 07:52

    I have resolved it by adding below line in my context:

    modelBuilder.Entity<YourObject>().Property(e => e.YourColumn).HasMaxLength(4000);
    

    Somehow, [MaxLength] didn't work for me.

    0 讨论(0)
  • 2020-12-04 07:59

    Following are the results when we use both [MaxLength] and [StringLength] attributes, in EF code first. If both are used, [MaxLength] wins the race. See the test result in studentname column in below class

     public class Student
     {
        public Student () {}
    
        [Key]
        [Column(Order=1)]
        public int StudentKey { get; set; }
    
        //[MaxLength(50),StringLength(60)]    //studentname column will be nvarchar(50)
        //[StringLength(60)]    //studentname column will be nvarchar(60)
        [MaxLength(50)] //studentname column will be nvarchar(50)
        public string StudentName { get; set; }
    
        [Timestamp]
        public byte[] RowVersion { get; set; }
     }
    
    0 讨论(0)
  • 2020-12-04 08:03

    All good answers...From the validation perspective, I also noticed that MaxLength gets validated at the server side only, while StringLength gets validated at client side too.

    0 讨论(0)
  • 2020-12-04 08:10

    MaxLengthAttribute means Max. length of array or string data allowed

    StringLengthAttribute means Min. and max. length of characters that are allowed in a data field

    Visit http://joeylicc.wordpress.com/2013/06/20/asp-net-mvc-model-validation-using-data-annotations/

    0 讨论(0)
提交回复
热议问题