Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

NetCore + Mysql + EF:No coercion operator is defined between types 'System.Int16' and 'System.Boolean',

ZpfCoder 2019-06-03 07:57:00 阅读数:231 评论数:0 点赞数:0 收藏数:0

总结三种解决办法:

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有其他解决办法吗?

 

版权声明
本文为[ZpfCoder]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/ZpfCoder/p/10968026.html