Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Data Annotations Set StringLength VarChar

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?

like image 474
AnonyMouse Avatar asked Sep 08 '11 00:09

AnonyMouse


People also ask

Which data annotation value specifies the max and min length of string with error?

The StringLength Attribute Specifies both the Min and Max length of characters that we can use in a field.

Which of the following annotations specifies the maximum string length allowed by the database column?

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.


1 Answers

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); 
like image 112
Eranga Avatar answered Sep 25 '22 12:09

Eranga