c# - Entity Framework MySQL tinyint(1) System.Boolean.Parse FormatException

标签 c# mysql entity-framework visual-studio-2013

我在使用 MySQL 数据库的 C# model-first 项目中使用 EntityFramework 6 一切都很好,我可以毫无问题地生成我的数据库。

然后我使用设计器修改了我的 .edmx 文件,这里开始出现我遇到的问题。

  • 首先,设计者不再更新 .edmx 文件的 CSDL 内容C-S 映射内容 部分。 所以我自己更新了内容,终于可以编译项目了。

这是 .edmx 文件,它现在的样子以及它在设计器中的样子:

EDMX 文件:http://pastebin.com/Xer9UyNR

这是设计器 View 的链接: http://i.stack.imgur.com/Vcv9W.png

  • 第二个(也是最重要的一个),当 EF 试图从我的数据库中获取一个 tinyint 并将其类型更改为 bool 值时,我得到一个 FormatException。
    à ArmoireOutils.App.OnNavigateMessageHandler(OnNavigateMessage message) dans c:\Users\JB\Desktop\CodingFrance\ArmoireOutils\ArmoireOutils\App.xaml.cs:line 101System.FormatException: String was not recognized as a valid Boolean..
    à System.Boolean.Parse(String value)
    à System.String.System.IConvertible.ToBoolean(IFormatProvider provider)
    à System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    à MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type targetType)
    à MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal)
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetUntypedValueDefault(DbDataReader reader, Int32 ordinal)
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
    à lambda_method(Closure , Shaper )
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
    à lambda_method(Closure , Shaper )
    à System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MaterializeRow()
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MoveNext()
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.TryReadToNextElement()
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.ReadElement()
    à System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.MoveNext()
    à System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
    à System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
    à System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
    à System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
    à System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
    à System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
    à System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
    à ArmoireOutils.Services.DataService.GetCupboardByGuid(String guid) dans c:\Users\JB\Desktop\CodingFrance\ArmoireOutils\ArmoireOutils\Services\DataService.cs:line 202

Here is my GetCupboardByGUID method:

public Cupboard GetCupboardByGuid(String guid)
    {
        using (var context = new ArmoireOutilsEntities())
        {
            var cupboard = (from a in context.Cupboards
                where a.GUID.Equals(guid)
                select a)
                .Include("ResidentTools")
                .Include("Tools")
                .Include("Users") //If I remove this, .SingleOrDefault() works fine.
                .SingleOrDefault(); //Throw FormatException when getting the User.Active value from the database.

            if (cupboard != null)
                cupboard.RefreshLists();

            return cupboard;
        }
    }

这是由 .edmx tt 生成的我的用户类:

public partial class User
{
    public User()
    {
        this.Tools = new ObservableCollection<Tool>();
        this.Cupboards = new ObservableCollection<Cupboard>();
        this.Active = true;
    }

    public int Id { get; set; }
    public short Type { get; set; }
    public string Firstname { get; set; }
    public string LastName { get; set; }
    public string Login { get; set; }
    public short Gender { get; set; }
    public short LangId { get; set; }
    public string Photo { get; set; }
    public System.DateTime CreationDate { get; set; }
    public Nullable<System.DateTime> ModificationDate { get; set; }
    public Nullable<System.DateTime> LastConnection { get; set; }
    public Nullable<System.DateTime> DisableDate { get; set; }
    public bool Active { get; set; }

    public virtual Lang Lang { get; set; }
    public virtual IList<Tool> Tools { get; set; }
    public virtual IList<Cupboard> Cupboards { get; set; }
}

所以我猜 EF 正在迭代数据库中 cupboarduser 中的所有用户(该表将 user 链接到 cupboard对于多对多关系),当涉及到为第一个用户设置 Active 值时,它首先从数据库中获取 1 作为 String 并且然后尝试使用 System.Boolean.Parse 将该字符串解析为 bool 值,但 thaat 方法不支持像“1”这样的数字表示 true(数据库中的字段是 tinyint(1 )).

那么为什么 EF 无法理解它是一个 tinyint,所以他不能在 System.Boolean.Parse 中使用它?

我试图从数据库中重新生成整个 .edmx 文件 => 同样的异常

我试图从头开始重新生成整个 .edmx 文件 => 同样的异常

我不明白为什么因为我没有修改用户模型所以 Active 字段已经存在并且工作得很好。

抱歉发了这么长的帖子,提前致谢。

最好的问候, 平民

最佳答案

在特定实体上配置数据类型:

modelBuilder.Entity<User>()
                  .Property(p => p.Active)
                  .HasColumnType("bit");

或一般:

modelBuilder.Properties()
            .Where(x => x.PropertyType == typeof(bool))
            .Configure(x => x.HasColumnType("bit"));

关于c# - Entity Framework MySQL tinyint(1) System.Boolean.Parse FormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021287/

相关文章:

php - 使用 mysql 连接 android 和 php 时发生错误

c# - EF 与缓慢变化维度的关系

c# - 更新 edmx 模型后,DbContext 呢?

c# - 如何从随机池中选择一个号码,然后让号码不能重新选择

mysql - 寻址三个表 : How many from A are not in B or C? 的 MySQL 查询

c# - Entity Framework - 未映射的实体。可能的?

c# - EntityFramework 和级联删除

c# - Task.WhenAny 会取消注册未完成任务的延续吗?

c# - USPS 地址验证 API : If & or # symbol in address field then API returns error response

mysql - 将数据从 MySQL 5.7 降级到 MySQL 5.1