Entity Framework Data Annotations Set StringLength VarChar

entity-framework

Question

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?

1
38
9/8/2011 12:01:25 AM

Accepted Answer

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);
86
4/10/2013 7:45:32 AM

Popular Answer

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow