简体   繁体   English

EF Core至Mysql表绑定

[英]EF Core to Mysql table binding

I have the following table in MySQL 我在MySQL中有下表 在此处输入图片说明

When I run the following code in some middleware 当我在某些中间件中运行以下代码时

var apiKeys = _appContext.apikey.ToList();

I get this error 我得到这个错误

System.InvalidOperationException: No coercion operator is defined between types 'System.Int16' and 'System.Boolean'. System.InvalidOperationException:类型'System.Int16'和'System.Boolean'之间没有定义强制操作符。

This is my ApiKey class 这是我的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; }
}

I had this working with a Postgresql db and just moved over to MySQL. 我曾与Postgresql db一起工作过,然后才移到MySQL。 Does this have something to do with going from tinyint (in the db) to bool (in the class)? 这与从tinyint(在数据库中)到bool(在类中)有关吗?

I'm using MySql.Data.EntityFrameworkCore 8.0.13 我正在使用MySql.Data.EntityFrameworkCore 8.0.13

2 possible options as answered in comments already. 评论中已经回答了2个可能的选项。

  • Pomelo driver supports bool to int mapping, Pomelo驱动程序支持bool到int的映射,
  • Second option would be using value converters , which works with the other driver. 第二种选择是使用与其他驱动程序一起使用的值转换器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM