简体   繁体   中英

How do I specify the maximum column length of a field using entity framework code first

Is there an attribute I can use when creating a table ? I tried [StringLength] but it seems to be ignored.

 public class EntityRegister
 {
     public int Id { get; set; }
     [StringLength(450)]
     public string Name { get; set; }
 }

alternatively, you can manually do it on Fluent API

use HasMaxLength(450)

  • Configuring Properties and Types with the Fluent API

or if you want Data Annotation , use MaxLength and MinLength attributes

public class EntityRegister
{
    public int Id { get; set; }
    [MaxLength(450)]
    public string Name { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM