Entity Framework Data Annotations Set StringLength VarChar

Entity Framework

Entity Framework Problem Overview


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?

Entity Framework Solutions


Solution 1 - Entity Framework

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);

Solution 2 - Entity Framework

For some reason this older post keeps coming up in my search... so just FYI, using EF6 Core it's combined. The above answer errors out for me.

[Column(TypeName = "VARCHAR(250)")]
public string Comment {get;set;}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnonyMouseView Question on Stackoverflow
Solution 1 - Entity FrameworkErangaView Answer on Stackoverflow
Solution 2 - Entity Frameworkamd3View Answer on Stackoverflow