【发布时间】:2019-10-18 20:28:36
【问题描述】:
我正在使用 .net core 2.2 和使用 MySql.Data.EntityFramework.Core 的实体框架。当我执行以下操作时遇到此异常:
DbContext.MyDbSet.Add(new MyClass());
DbContext.SaveChanges();
例外:
System.InvalidCastException:无法转换类型的对象 System.Boolean 类型 System.Int16
我在 OnModelCreating 函数中添加了以下代码 sn-p:
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
foreach (var property in entityType.GetProperties())
if (property.ClrType == typeof(bool) || property.ClrType == typeof(Boolean))
property.SetValueConverter(new BoolToZeroOneConverter<Int16>());
我错过了什么?
【问题讨论】:
-
我认为你的模型和数据库类型不匹配。
-
同意,确保您的 C# 模型和数据库字段具有相同的类型是有意义的。换一个合适的。那么你就没有问题了。
-
我认为它是 MySql 包 看看这个github.com/abpframework/abp/issues/866
-
@AhmetArslan - 你应该添加你的评论作为答案 - 这是这个问题的正确答案
标签: c# mysql exception entity-framework-core