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

C#Sql ServerEntity FrameworkEf Code-First

C# Problem Overview


I've been searching around the web trying to figure out the right syntax to have Entity Framework Code First create my table with a column: varchar(max).

This is what I have. By default this creates varchar(128). How do I create varchar(max)?

I have tried [MaxLength] without success.

Any help would be appreciated. Thanks!

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

C# Solutions


Solution 1 - C#

[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.

Solution 2 - C#

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

Solution 3 - C#

Use [MaxLength] annotation.

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

Solution 4 - C#

Update for @Slauma answer.

Using a override for all strings like this in OnModelCreating:

modelBuilder.Properties<string>().Configure(s => 
    s.HasMaxLength(256).HasColumnType("nvarchar"));

and then modifying properties with attributes like this:

[Column(TypeName = "nvarchar(MAX)")]
public string CaseComment { get; set; }

Or this:

modelBuilder.Entity<YourClass>()
    .Property(b => b.CaseComment)
    .HasColumnType("nvarchar(MAX)");

This can cause the Exception. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Even though the column is of correct data type Entity Framework still thinks it is nvarchar(256) and throws the error DbEntityValidationException.

To fix this use the following instead:

[Column(TypeName = "nvarchar(MAX)")]
[MaxLength]
public string CaseComment { get; set; }

Or

modelBuilder.Entity<YourClass>()
    .Property(b => b.CaseComment)
    .HasColumnType("nvarchar(MAX)")
    .HasMaxLength(null);

Solution 5 - C#

[Column(TypeName = "varchar(MAX)")] results in varchar(Max) in the database. If you have a custom convention setting strings MaxLength to 100,

modelBuilder
    .Properties<string>()
    .Configure(c => c.IsUnicode(false).HasMaxLength(100));

you need to include the MaxLength attribute [Column(TypeName = "varchar(MAX)"), MaxLength]. If you don't, you will get varchar(MAX) in the database but validation will throw for more than 100 characters.

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
QuestionMaddhacker24View Question on Stackoverflow
Solution 1 - C#SlaumaView Answer on Stackoverflow
Solution 2 - C#Diego MijelshonView Answer on Stackoverflow
Solution 3 - C#bhuvinView Answer on Stackoverflow
Solution 4 - C#OgglasView Answer on Stackoverflow
Solution 5 - C#cResultsView Answer on Stackoverflow