【问题标题】:.Net Core2.2 : how to change characterset in EF codefirst approach.Net Core 2.2:如何在 EF 代码优先方法中更改字符集
【发布时间】:2019-03-03 13:33:52
【问题描述】:

我尝试在连接字符串中使用Charset=utf8; 并在我的模型中使用[MySqlCharset("utf8")](VsCode 无法识别)。 我也读过这个方法:

protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder.Entity<ComplexKey>(e =>
    {
      e.HasKey(p => new { p.Key1, p.Key2 });
      e.ForMySQLHasCollation("utf8"); // defining collation at Entity level
      e.Property(p => p.Key1).ForMySQLHasCharset("utf8"); // defining charset in a property
      e.Property(p => p.CollationColumnFA).ForMySQLHasCollation("utf8"); // defining collation in a property
    });
  }

但我没有测试它,因为我认为对每个表都这样做很乏味,应该有更简单的方法。

【问题讨论】:

    标签: c# mysql .net-core


    【解决方案1】:

    只需将字符集插入到 MySql 的连接字符串中(在您的数据库上下文类中),然后您就不需要在任何属性上声明字符集。

    例子:

    
    
    public  class YourDbContext : DbContext {
            private readonly IConfiguration _config;
    
            // See the charset in the end of this string.
            private string _yourConnectionString = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=utf8;";
    
            protected override void OnConfiguring(DbContextOptionsBuilder builder) {
                builder.UseMySQL( _yourConnectionString  );
            }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 2021-01-08
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2019-08-26
      • 1970-01-01
      • 2019-03-23
      相关资源
      最近更新 更多