EF Core到Mysql表的绑定 [英] EF Core to Mysql table binding

查看:142
本文介绍了EF Core到Mysql表的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL中有下表

I have the following table in MySQL

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

When I run the following code in some middleware

var apiKeys = _appContext.apikey.ToList();

我收到此错误

System.InvalidOperationException:类型'System.Int16'和'System.Boolean'之间没有定义强制操作符.

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

这是我的ApiKey类

This is my 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(在类中)有关吗?

I had this working with a Postgresql db and just moved over to MySQL. Does this have something to do with going from tinyint (in the db) to bool (in the class)?

我正在使用MySql.Data.EntityFrameworkCore 8.0.13

推荐答案

2个可能的选项已经在注释中得到了回答.

2 possible options as answered in comments already.

  • Pomelo driver supports bool to int mapping,
  • 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 >();

这篇关于EF Core到Mysql表的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆