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 >();
来源:https://stackoverflow.com/questions/53473726/ef-core-to-mysql-table-binding