How do I set a column in SQL Server to varchar(max) using ASP.net EF Codefirst Data Annotations? How do I set a column in SQL Server to varchar(max) using ASP.net EF Codefirst Data Annotations? sql-server sql-server

How do I set a column in SQL Server to varchar(max) using ASP.net EF Codefirst Data Annotations?


[Column(TypeName = "varchar(MAX)")]

Surprisingly the most obvious solution works.

The [MaxLength] attribute only creates a varchar column with a max length that isn't MAX but - in my case (SQL Server Express 2008 R2) - 8000.


This will get you nvarchar(max):

[StringLength(int.MaxValue)]

I don't think there's an attribute to force non-unicode (are you sure you want that?), so for varchar(max) you need a tweak in the DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder){    modelBuilder.Entity<Entity>().Property(x => x.MediaDesc).IsUnicode(false);}


Use [MaxLength] annotation.

[Column(TypeName = "varchar")][MaxLength]public string MediaDesc { get; set; }