Entity Framework Data Annotations Set StringLength VarChar

后端 未结 2 1287
小鲜肉
小鲜肉 2020-12-01 18:05

I have this setting in my model:

[StringLength(250)]
public string Comment { get; set; }

to set the maximum length to 250 in the database w

相关标签:
2条回答
  • 2020-12-01 18:11

    For some reason this older post keeps coming up in my search... so just FYI, using EF6 Core it's combined. The above answer errors out for me.

    [Column(TypeName = "VARCHAR(250)")]
    public string Comment {get;set;}
    
    0 讨论(0)
  • 2020-12-01 18:13

    Use ColumnAttribute to give the datatype

    [Column(TypeName = "VARCHAR")]
    [StringLength(250)]
    public string Comment { get; set; }
    

    Or use fluent API

    modelBuilder.Entity<MyEntity>()
      .Property(e => e.Comment).HasColumnType("VARCHAR").HasMaxLength(250);
    
    0 讨论(0)
提交回复
热议问题