首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >StringLength vs MaxLength attributes ASP.NET MVC与实体框架EF代码优先

StringLength vs MaxLength attributes ASP.NET MVC与实体框架EF代码优先
EN

Stack Overflow用户
提问于 2011-04-19 21:23:23
回答 6查看 129.8K关注 0票数 165

[MaxLength][StringLength]属性的行为有什么不同?

据我所知(除了[MaxLength]可以验证数组的最大长度之外),它们是相同的并且有点冗余吗?

EN

回答 6

Stack Overflow用户

发布于 2017-01-06 22:51:47

以下是在EF code first中同时使用[MaxLength][StringLength]属性时的结果。如果两者都使用,[MaxLength]就会赢得比赛。查看下面类中studentname列中的测试结果

代码语言:javascript
复制
 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; }
 }
票数 14
EN

Stack Overflow用户

发布于 2014-11-21 06:55:34

从验证的角度来看,一切都很好,我还注意到MaxLength只在服务器端得到验证,而StringLength也在客户端得到验证。

票数 12
EN

Stack Overflow用户

发布于 2014-07-31 16:13:14

MaxLengthAttribute的意思是Max允许的数组或字符串数据长度

StringLengthAttribute的意思是最小。和最大。数据字段中允许的字符长度

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5717033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档