E-Bat
E-Bat

Reputation: 4892

Entity Framework 6 code first: setting unicode to false for string properties

In my model i have some entities decorated with StringLength attribute:

[StringLength(128)]    
public string FirstName { get; set; }

Also i have disable unicode for all string properties this way:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Properties<string>().Configure(p => p.IsUnicode(false));            
}

The problem is that all string properties decorated with the mentioned attribute are ignoring this setting when generating the database schema, producing nvarchar datatype for the corresponding database columns. What is the correct way to disable unicode in this cases?

Upvotes: 16

Views: 20838

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109119

Seems to be a bug (or omission) in the new PropertyConventionConfiguration API. The following configuration does work, so it can serve as a work-around:

modelBuilder.Properties<string>().Configure(x => x.HasColumnType("VARCHAR"));

Upvotes: 15

Related Questions