I have this setting in my model:
[StringLength(250)] public string Comment { get; set; }
to set the maximum length to 250 in the database which is great.
However it's set as nvarchar(250) when the database person was expecting varchar(250).
Can somebody please tell me how to set it as a varchar from the model as opposed to an nvarchar?
The StringLength Attribute Specifies both the Min and Max length of characters that we can use in a field.
Data Annotations - StringLength Attribute in EF 6 & EF Core It specifies the maximum characters allowed for a string property which in turn sets the size of a corresponding column ( nvarchar in SQL Server) in the database.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With