简体   繁体   English

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

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

I have tried using Charset=utf8; 我尝试使用Charset=utf8; in the connection string and using [MySqlCharset("utf8")] in my model(which was not recognized by VsCode). 在连接字符串中并在模型中使用[MySqlCharset("utf8")] (VsCode无法识别)。 I have also read about this method: 我也了解了这种方法:

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

but I have not tested it because I think it is tedious to do that for every single table and there should be an easier way. 但是我没有测试它,因为我认为对每个表执行此操作很繁琐,应该有一种更简单的方法。

Just insert the charset inside your connection string to MySql ( in your database context class ), and then you will not need to declare the charset on any property. 只需将连接集内的字符集插入MySql(在数据库上下文类中),然后就无需在任何属性上声明该字符集。

Example: 例:



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.

相关问题 如何在ASP.Net Core2.2中覆盖415响应 - How to Override 415 response in ASP.Net Core2.2 创建模型时不能使用上下文。 EF-核心ASP.net Core2.2 - The context cannot be used while the model is being created. EF-Core ASP.net Core2.2 如何在 core2.2 mvc 项目中使用 IClaimsTransformation - how to use IClaimsTransformation in core2.2 mvc project 无法使用 Firefox 调试某些 asp.net core2.2 网页。 如何使用 Firefox 进行调试? - Unable to debug some asp.net core2.2 web pages with Firefox. How can I debug with Firefox? EF Core2.2:Scaffold-DbContext无法在WPF项目中使用命名连接字符串 - EF Core2.2: Scaffold-DbContext not working with named connection string in WPF project 使用HttpClient Alwyes Null ASP.net Core2.2将IFormFile发送到API - Send IFormFile To API using HttpClient Alwyes Null ASP.net Core2.2 使用 ASP.NET Core2.2 中的 CancellationToken 取消内部任务 - Cancel an internal task using CancellationToken in ASP.NET Core2.2 无法使用 dinktopdf 为 .net core2.2 azure functionapp 加载 dll libwkhtmltox - Unable to load the dll libwkhtmltox using dinktopdf for a .net core2.2 azure functionapp 当FromBody MVC Core2.2剂量工作时出现错误请求 - Bad Request whenFromBody mvc core2.2 dosent work “dotnet ef”命令在 .NET Core 2.2 中不起作用 - 'dotnet ef' command is not working in .NET Core 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM