我尝试在连接字符串中使用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
});
}
但是我没有测试它,因为我认为对每个表执行此操作很繁琐,应该有一种更简单的方法。
答案 0 :(得分:0)
只需将连接集内的字符集插入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 );
}
}