EF Core 到 Mysql table 绑定

EF Core to Mysql table binding

我在 MySQL 中有以下 table

当我在某些中间件运行下面的代码

var apiKeys = _appContext.apikey.ToList();

我收到这个错误

System.InvalidOperationException: No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.

这是我的 ApiKey class

public class ApiKey
{
    public string apikeyid { get; set; }
    public string uid { get; set; }
    public string apikey { get; set; }
    public bool isactive { get; set;}
    public bool ispaid { get; set; }
    public bool ismod { get; set; }
    public bool isadmin { get; set; }
}

我使用 Postgresql 数据库进行了此操作,然后转移到 MySQL。这与从 tinyint(在数据库中)到 bool(在 class 中)有关吗?

我正在使用 MySql.Data.EntityFrameworkCore 8.0.13

2 个可能的选项已在评论中回答。

  • Pomelo driver支持bool到int的映射,
  • 第二个选项是使用 value converters,它与其他驱动程序一起使用。

    entity.Property(p => p.isActive).HasConversion< int >();