[英].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.