locked
how to set the length of column for text in mysql RRS feed

  • Question

  • User-1134715901 posted

    Hi All,

    I am learning  asp.net core + entity framework core + mysql  to build cross-platform solution. I use Entity Framework core with code first and I find that the property with string type in class is mapped to "text" in mysql database, but i want to know that how to set the length of column in mapping table.

    Sunday, February 18, 2018 6:05 AM

Answers

  • User1400794712 posted

    Hi gradylu,

    When we use EF code first, we can use Data Annotation to add some limitation to properties.

    About the length of column, there are three related attributes:

    MinLength

    Can be applied to a property to specify the minimum string length allowed in the corresponding column in the database.

    MaxLength

    Can be applied to a property to specify the maximum string length allowed in the corresponding column in the database.

    StringLength

    Can be applied to a property to specify maximum string length allowed corresponding column in the database.

    For example:

    [MaxLength(10)]
    public string Title { get; set; }
    

    Then in database, this column should be no more than 10 char.

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 6:47 AM
  • User-1134715901 posted

    Thank you, I found out the issue, because I need store very long string to database, so I change data type into 'mediumtext';

    I record the issue to my blog: http://www.guavo.cn/article/6

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 1:23 PM

All replies

  • User1400794712 posted

    Hi gradylu,

    When we use EF code first, we can use Data Annotation to add some limitation to properties.

    About the length of column, there are three related attributes:

    MinLength

    Can be applied to a property to specify the minimum string length allowed in the corresponding column in the database.

    MaxLength

    Can be applied to a property to specify the maximum string length allowed in the corresponding column in the database.

    StringLength

    Can be applied to a property to specify maximum string length allowed corresponding column in the database.

    For example:

    [MaxLength(10)]
    public string Title { get; set; }
    

    Then in database, this column should be no more than 10 char.

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 6:47 AM
  • User-1134715901 posted

    Thank you, I found out the issue, because I need store very long string to database, so I change data type into 'mediumtext';

    I record the issue to my blog: http://www.guavo.cn/article/6

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 1:23 PM