EF Core 到 Mysql 表绑定 [英] EF Core to Mysql table binding
问题描述
我在 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 db 进行了此操作,然后转移到了 MySQL.这与从tinyint(在db中)到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 支持 bool 到 int 的映射,
第二个选项是使用值转换器,它与其他驱动程序一起使用.
- 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
entity.Property(p => p.isActive).HasConversion< int >();
这篇关于EF Core 到 Mysql 表绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!