实体框架4 unicode问题保存

时间:2011-01-29 09:19:46

标签: asp.net mysql asp.net-mvc entity-framework-4

我在使用带有MySQL连接器6.3.6的Entity Framework 4时遇到问题。我已经将MySQL dabase设为utf8 - 默认排序规则,以便在我的网站上使用unicode。问题是,当我尝试使用实体框架4保存unicode字符串时,我可以从数据库中读取unicode到我的网站,它会在我的数据库上放置问号。我甚至从Visual Studio设计器设置了Unicode属性但无济于事。我是否遗漏了某些内容,或者您​​需要做些什么来使实体框架将unicode写入数据库?

3 个答案:

答案 0 :(得分:4)

好了很多研究后发现MySQL连接器6.3.6得到了一个连接字符串参数'charset = utf8'。你需要将它添加到你的连接字符串。

答案 1 :(得分:1)

  

在Web.config文件中使用'charset = utf8'创建连接字符串

<connectionStrings>
<add name="con" connectionString="server=localhost;Uid=root;password=;database=webpro;charset=utf8 "  />    
</connectionStrings>

答案 2 :(得分:0)

在我的情况下,以某种方式在上下文中禁用了unicode配置,这里的解决方案是为.IsUnicode(true)而不是.IsUnicode(false)设置true值

 modelBuilder.Entity<Center>()
            .Property(e => e.CenterCode)
            .IsUnicode(true);

让我们享受这个技巧。