简体   繁体   中英

EF Core to Mysql table binding

I have the following table in 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'.

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; }
}

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)?

I'm using MySql.Data.EntityFrameworkCore 8.0.13

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 >();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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