Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 593
E-Bat Avatar asked Mar 01 '14 20:03

E-Bat


People also ask

What is IsUnicode?

IsUnicode() Configures the column to support Unicode string content. IsUnicode(Nullable<Boolean>) Configures whether or not the column supports Unicode string content.

Which of the following is the default convention to configure a Primarykey property in EF 6 code first?

Primary Key Convention Code First infers that a property is a primary key if a property on a class is named “ID” (not case sensitive), or the class name followed by "ID". If the type of the primary key property is numeric or GUID it will be configured as an identity column.

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.


1 Answers

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"));
like image 51
Gert Arnold Avatar answered Oct 19 '22 14:10

Gert Arnold