c# - EF Core 到 Mysql 表绑定(bind)

标签 c# mysql .net-core ef-core-2.1

我在 MySQL 中有下表 enter image description here

当我在一些中间件中运行以下代码时

var apiKeys = _appContext.apikey.ToList();

我收到这个错误

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

这是我的 ApiKey 类

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(在类中)有什么关系吗?

我正在使用 MySql.Data.EntityFrameworkCore 8.0.13

最佳答案

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

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

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

关于c# - EF Core 到 Mysql 表绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53473726/

相关文章:

PHP下拉列表不选择值

c# - ASP Core 无法在 VS 2017 中设置用户 secret

c# - 组合框选择的索引更改事件未在代码中触发

c# - 如何在 C# 中的 azure 认知搜索结果中返回 search.score

c# - 如何在 EF 中批量更新

php - MYSQL/PHP - 在值不变时返回行的平均值?

MySQL 行和列选择

c# - 检查 C# .NET Core 中的文件权限

c# - CredentialCache.DefaultCredentials 不遵守 dotnet core 2.2 的 IIS App Pool Credentials

c# - WPF C# - 从另一个线程更新进度条