gpt4 book ai didi

c# - .Net核心2.2 : how to change characterset in EF codefirst approach

转载 作者:行者123 更新时间:2023-11-29 16:14:18 24 4
gpt4 key购买 nike

我尝试在连接字符串中使用 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 );
}
}

关于c# - .Net核心2.2 : how to change characterset in EF codefirst approach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54969360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com