实体框架6代码首先:为字符串属性设置unicode为false [英] Entity Framework 6 code first: setting unicode to false for string properties

查看:542
本文介绍了实体框架6代码首先:为字符串属性设置unicode为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此外,我已经为所有字符串属性禁用unicode这样:

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

问题是所有使用上述属性装饰的字符串属性都忽略此设置当生成数据库模式时,为相应的数据库列生成nvarchar数据类型。
在这种情况下,禁用unicode的正确方法是什么?

解决方案

似乎是新的 PropertyConventionConfiguration中的错误(或省略) API。以下配置可以正常工作,因此可以作为解决方案:

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


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?

解决方案

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

这篇关于实体框架6代码首先:为字符串属性设置unicode为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆