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

故事扮演 提交于 2019-12-31 10:41:57

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/22120007/entity-framework-6-code-first-setting-unicode-to-false-for-string-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!