如何首先使用实体 框架代码指定字段的最大列长度

问题描述 投票:31回答:1

是否有创建表时,我可以使用属性?我试图[StringLength]但它似乎被忽略。

 public class EntityRegister
 {
     public int Id { get; set; }
     [StringLength(450)]
     public string Name { get; set; }
 }
c# .net entity-framework entity-framework-5 system.componentmodel
1个回答
58
投票

另外,您也可以手动执行它Fluent API

使用HasMaxLength(450)

或者如果你想Data Annotation,使用MaxLengthMinLength属性

public class EntityRegister
{
    public int Id { get; set; }
    [MaxLength(450)]
    public string Name { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.