簡體   English   中英

.Net Core2.2:如何在EF Codefirst方法中更改字符集

[英].Net Core2.2 : how to change characterset in EF codefirst approach

我嘗試使用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
    });
  }

但是我沒有測試它,因為我認為對每個表執行此操作很繁瑣,應該有一種更簡單的方法。

只需將連接集內的字符集插入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  );
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM