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?
IsUnicode() Configures the column to support Unicode string content. IsUnicode(Nullable<Boolean>) Configures whether or not the column supports Unicode string content.
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.
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.
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With