NetCore + Mysql + EF:No coercion operator is defined between types 'System.Int16' and 'System.Boolean',
总结三种解决办法:
1.Mysql升级到7
2.Nuget安装Pomelo.EntityFrameworkCore.MySql 2.2.0替代MySql.Data.EntityFrameworkCore 8.0.16后正常.
PS:如果使用Pomelo库的SQL语句查询Convert.ToDateTime(item["createtime"]).ToString("yyyy-MM-dd HH:mm:ss")会报Unable to cast object of type 'MySql.Data.Types.MySqlDateTime' to type 'System.IConvertible'. 要改成Convert.ToDateTime(item["dskuupdatetime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss").
3.EF上下文添加转换代码
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder); foreach (var entityType in builder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
if (property.ClrType == typeof(bool))
{
property.SetValueConverter(new BoolToIntConverter());
}
}
}
}
public class BoolToIntConverter : ValueConverter<bool, int>
{
public BoolToIntConverter( ConverterMappingHints mappingHints = null)
: base(
v => Convert.ToInt32(v),
v => Convert.ToBoolean(v),
mappingHints)
{
} public static ValueConverterInfo DefaultInfo { get; }
= new ValueConverterInfo(typeof(bool), typeof(int), i => new BoolToIntConverter(i.MappingHints));
}
PS: Linq Any转换失败:
var isExist = db.MyDbTable.Any(o => o.Id == id);
//throw No coercion operator is defined between types 'System.Int16' and 'System.Boolean',
要改成
var isExist = db.MyDbTable.FirstOrDefault(o => o.Id == id) != null;
对于Linq Any有其他解决办法吗?
NetCore + Mysql + EF:No coercion operator is defined between types 'System.Int16' and 'System.Boolean',的更多相关文章
- efcore操作mysql,出现System.InvalidOperationException:“No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.”
这个恶心的问题,只需要把EF的依赖换成 Pomelo.EntityFrameworkCore.MySql 库即可解决
- System.InvalidOperationException:“No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.”
modelBuilder.Entity<MentItems>().Property(e=>e.IsValid) .HasColumnType("bit(1)") ...
- NetCore+MySql+EF 数据库生成实体模型
NetCore版本 2.1 1.点击“工具”->“NuGet包管理器”->“程序包管理器控制台” 分别安装以下几个包 Mysql 版本: MySql.Data.EntityFrame ...
- .NetCore之EF跳过的坑
我在网上看到很多.netCore的信息,就动手自己写一个例子测试哈,但是想不到其中这么多坑: 1.首先.netCore和EF的安装就不用多说了,网上有很多的讲解可以跟着一步一步的下载和安装,但是需要注 ...
- VS2015+MySql EF的配置问题
自己做笔记,防止以后各种找! 去MySql下载最新版的安装包,MySql For Windows全部就可以了,根据开发需求安装功能,然后安装MySql的步骤上网去找一大堆. 注意事项: 第一:必须把V ...
- asp.net core + mysql + ef core + linux
asp.net core + mysql + ef core + linux 以前开发网站是针对windows平台,在iis上部署.由于这次需求的目标服务器是linux系统,就尝试用跨平台的.NET ...
- MySql+EF <二>
C#使用Mysql+EF架构项目有一系列问题. 一.EF没有Mysql的驱动,这个需要自己安装2个插件 ①mysql-connector-net-6.9.10.msi ②mysql-for-visua ...
- .NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序
先贴上解决方案截图 一.新建4个解决方案文件夹 1-Presentation 2-Application 3-Domain 4-Infrastructure 二.在解决方案文件夹中分别创建项目 其余项 ...
- MySQL not equal to operator <> && !=
MySQL :: MySQL 5.7 Reference Manual :: 12.3.2 Comparison Functions and Operatorshttps://dev.mysql.co ...
随机推荐
- [egret+pomelo]实时游戏杂记(1)
[egret+pomelo]学习笔记(1) [egret+pomelo]学习笔记(2) [egret+pomelo]学习笔记(3) 资料 egret pomelo pomelo捡宝项目 准备工作 1. ...
- 解决ubuntu12.04下安装gitlabError Compiling CSS asset的错误以及401资源错误
安装过程 https://www.gitlab.com.cn/installation/#ubuntu 解决过程 12.04ubuntu坑太多 解决有用的链接如下 https://blog.csdn. ...
- Git查看并修改name和email
显示name的方法: git config user.name git config --list 或者查看~/.gitconfig文件. 改名字: git config --global user. ...
- python源码安装的包的卸载
python setup.py install安装的包如何卸载 在使用源码安装的过程中,记录安装文件细节,如: python setup.py install --record log 这时所有的安装 ...
- c# json 排版
public static string PraseToJson(string json) { try { JsonSerializer s = new JsonSerializer(); JsonR ...
- PHP留言小练习
实现功能: 留言.搜索.编辑.删除.详情页.时间.点击量 页面划分: index.html(留言列表页) add.html(留言页) edit.php(编辑页) del.php(删除页) view.p ...
- RTSP协议
1.RTSP与几个相关协议 RTSP(Real Time Streaming Protocol)实时流协议,是用来控制声音或影像的多媒体串流协议,并允许同时多个串流需求控制,传输时所用的网络通 ...
- 1087 All Roads Lead to Rome (30)(30 分)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- ACM学习历程—HDU 1059 Dividing(dp && 多重背包)
Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...
- 1068 Bash游戏 V3
1068 Bash游戏 V3 题目来源: Ural 1180 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 有一堆石子共有N个.A B两个人轮流拿 ...