2

what is DataAnnotation attribute i can use to make gander column only have one character in my table

public class Student
 {
      public int ID { get; set; }
      [Required, MaxLength(50)]
      public string Name { get; set; }
      [DataType(DataType.Date)]
      public DateTime Birthday { get; set; }

      public char Gander { get; set; }

 }
Tarek Saied
  • 6,482
  • 20
  • 67
  • 111

2 Answers2

0

Use the Column attribute.

 public class Student
 {
      public int ID { get; set; }
      [Required, MaxLength(50)]
      public string Name { get; set; }
      [DataType(DataType.Date)]
      public DateTime Birthday { get; set; }

      [Column(TypeName = "NVARCHAR(1)")]
      public char Gander { get; set; }

 }
Eranga
  • 32,181
  • 5
  • 97
  • 96
0

This should work:

[MaxLength(1)]
public string Gender { get; set; }

The problem is that char is not supported type in mapping and without change in EF core to support the type directly or introducing some simple type mapping or mapped conversions you are not able to map such property.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670