在 MySQL Linq Query EF6 中使用多个 Include() 方法时出现 FormatException

Posted

技术标签:

【中文标题】在 MySQL Linq Query EF6 中使用多个 Include() 方法时出现 FormatException【英文标题】:FormatException when using multiple Include() methods in MySQL Linq Query EF6 【发布时间】:2015-04-25 10:51:00 【问题描述】:

我正在尝试执行 LINQ 查询并多次使用Include() 来提取相关信息。

如果我执行以下查询,它可以正常工作:

var usersQuizzes = this.Context.EntitySet<Quiz>()
                    .Include(x => x.Questions)
                    .Include(x => x.QuizVersion)
                    .Include(x => x.QuizPasswords)
                    .Include(x => x.QuizVersion.Questions)
                    .Take(20)
                    .ToList();

但如果我有这样的错误:

var usersQuizzes = this.Context.EntitySet<Quiz>()
                    .Include(x => x.Questions)
                    .Include(x => x.QuizVersion)
                    .Include(x => x.QuizPasswords)
                    .Include(x => x.QuizVersion.Questions)
                    .Include(x => x.QuizVersions) // Added this
                    .Take(20)
                    .ToList();

或者这个:

var usersQuizzes = this.Context.EntitySet<Quiz>()
                    .Include(x => x.Questions)
                    .Include(x => x.QuizVersion)
                    .Include(x => x.QuizPasswords)
                    .Include(x => x.QuizVersion.Questions)
                    .Include(x => x.ResultDescriptions) // Added this instead
                    .Take(20)
                    .ToList();

或者这个:

 var usersQuizzes = this.Context.EntitySet<Quiz>()
                        .Include(x => x.QuizVersions)
                        .Include(x => x.ResultDescriptions)
                        .Include(x => x.QuizPasswords)
                        .Take(20)
                        .ToList();

但如果我去掉 QuizPasswords 就可以了:

 var usersQuizzes = this.Context.EntitySet<Quiz>()
                        .Include(x => x.QuizVersions)
                        .Include(x => x.ResultDescriptions)
                        .Take(20)
                        .ToList();

错误信息

在 mscorlib.dll 中出现“System.FormatException”类型的异常 但未在用户代码中处理

附加信息:字符串未被识别为有效的布尔值。

Quiz.cs

public partial class Quiz
    
        public Quiz()
        
            this.TakerAnswers = new List<TakerAnswer>();
            this.FeaturedQuizzes = new List<FeaturedQuiz>();
            this.PersonalityOutcomes = new List<PersonalityOutcome>();
            this.PossibleAnswers = new List<PossibleAnswer>();
            this.Questions = new List<Question>();
            this.QuizLinks = new List<QuizLink>();
            this.QuizPasswords = new List<QuizPassword>();
            this.QuizSubmissions = new List<QuizSubmission>();
            this.QuizVersions = new List<QuizVersion>();
            this.ResultDescriptions = new List<ResultDescription>();
            this.Takers = new List<Taker>();
            this.PersonalityResultImages = new List<PersonalityResultImage>();
            this.PersonalityShareImages = new List<PersonalityShareImage>();
        

        public int Id  get; set; 
        public int UserId  get; set; 
        public Nullable<int> QuizVersionId  get; set; 
        public string IpAddress  get; set; 

        [DefaultValue(false)]
        public bool IsPublic  get; set; 

        [DefaultValue(QuizType.Scored)]
        public QuizType QuizType  get; set; 

        public int CreatedTime  get; set; 
        public Nullable<int> BackgroundId  get; set; 

        [Index(IsUnique = true)]
        public string UrlId  get; set; 

        [DefaultValue(false)]
        public bool PasswordsDisabled  get; set; 

        [DefaultValue(false)]
        public bool SharedFacebook  get; set; 

        [DefaultValue(false)]
        public bool SharedTwitter  get; set; 

        [DefaultValue(false)]
        public bool Deleted  get; set; 

        [DefaultValue(false)]
        public bool ShowCorrections  get; set; 

        [DefaultValue(true)]
        public bool ShowBreakdown  get; set; 
        public string Thumbnail  get; set; 
        public string ShareImage  get; set; 
        public Nullable<int> QuizLinkId  get; set; 

        [DefaultValue(false)]
        public bool ForceShareImage  get; set; 

        [DefaultValue(0)]
        public int ShareCount  get; set; 

        [DefaultValue(BackgroundAlign.Top)]
        public BackgroundAlign BackgroundAlign  get; set; 

        public string BackgroundColour  get; set; 
        public string TextColour  get; set; 
        public virtual ICollection<TakerAnswer> TakerAnswers  get; set; 
        public virtual ICollection<FeaturedQuiz> FeaturedQuizzes  get; set; 
        public virtual ICollection<PersonalityOutcome> PersonalityOutcomes  get; set; 
        public virtual ICollection<PossibleAnswer> PossibleAnswers  get; set; 
        public virtual ICollection<Question> Questions  get; set; 
        public virtual ICollection<QuizLink> QuizLinks  get; set; 
        public virtual ICollection<QuizPassword> QuizPasswords  get; set; 
        public virtual ICollection<QuizSubmission> QuizSubmissions  get; set; 
        public virtual ICollection<QuizVersion> QuizVersions  get; set; 
        public virtual QuizBackground QuizBackground  get; set; 
        public virtual QuizLink QuizLink  get; set; 
        public virtual QuizVersion QuizVersion  get; set; 
        public virtual User User  get; set; 
        public virtual ICollection<ResultDescription> ResultDescriptions  get; set; 
        public virtual ICollection<Taker> Takers  get; set; 
        public virtual ICollection<PersonalityResultImage> PersonalityResultImages  get; set; 
        public virtual ICollection<PersonalityShareImage> PersonalityShareImages  get; set; 
    

QuizVersion.cs

public partial class QuizVersion
    
        public QuizVersion()
        
            this.TakerAnswers = new List<TakerAnswer>();
            this.FeaturedQuizzes = new List<FeaturedQuiz>();
            this.PersonalityOutcomes = new List<PersonalityOutcome>();
            this.PossibleAnswers = new List<PossibleAnswer>();
            this.Questions = new List<Question>();
            this.QuizSubmissions = new List<QuizSubmission>();
            this.Takers = new List<Taker>();
        

        public int Id  get; set; 
        public int QuizId  get; set; 
        public string QuizName  get; set; 
        public string QuizIntro  get; set; 
        public string IpAddress  get; set; 
        public bool Public  get; set; 
        public int CreatedTime  get; set; 
        public virtual ICollection<TakerAnswer> TakerAnswers  get; set; 
        public virtual ICollection<FeaturedQuiz> FeaturedQuizzes  get; set; 
        public virtual ICollection<PersonalityOutcome> PersonalityOutcomes  get; set; 
        public virtual ICollection<PossibleAnswer> PossibleAnswers  get; set; 
        public virtual ICollection<Question> Questions  get; set; 
        public virtual Quiz Quiz  get; set; 
        public virtual ICollection<QuizSubmission> QuizSubmissions  get; set; 
        public virtual ICollection<Taker> Takers  get; set; 
    

ResultDescription.cs

public partial class ResultDescription

    public int Id  get; set; 
    public int QuizId  get; set; 
    public Nullable<int> Percentage  get; set; 
    public string Description  get; set; 

    [DefaultValue(false)]
    public bool Deleted  get; set; 

    public virtual Quiz Quiz  get; set; 

我认为这可能是我的数据库中的一些可疑值,但我检查了我的 POCO 类中的所有 bool 类型值,它们似乎都是类型 tinyint(1) 并设置为 10 并且没有空值。

什么可能导致这个错误?

【问题讨论】:

【参考方案1】:

在您的第一个错误示例中,我遇到了同样的问题并使用了此解决方法:

var usersQuizzes = this.Context.EntitySet<Quiz>()
                .Include(x => x.Questions)
                .Include(x => x.QuizVersion)
                .Include(x => x.QuizPasswords)
                .Include(x => x.QuizVersion.Questions);

this.Context.Entry(userQuizes).Collection("QuizVersions").Load();

userQuizes.Take(20).ToList();

【讨论】:

它可以工作,虽然没有人们希望的那么高效。最好添加错误报告以供历史参考。 bugs.mysql.com/bug.php?id=68886, bugs.mysql.com/bug.php?id=76466【参考方案2】:

我已经设法通过切换包含顺序并将有问题的包含放在第一位(在您的情况下是 x.QuizPasswords)来使我的代码工作

【讨论】:

【参考方案3】:

我找到了一个解决方案,强制属性“位”默认为“假”。我更改了 Mysql 中的数据类型。 Tinyint -> 位。

[Column("Active", TypeName = "bit")]
[DefaultValue(false)]
public bool Active  get; set; 

链接参考:

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

【讨论】:

以上是关于在 MySQL Linq Query EF6 中使用多个 Include() 方法时出现 FormatException的主要内容,如果未能解决你的问题,请参考以下文章

EF6 Dynamic Linq 将字符串转换为十进制

LINQ 和 EF6:创建模型时无法使用上下文

使用 EF6 优化 LINQ 查询

EF6 Linq 查询。仅包括返回子表的第一个条目

使用 linq 在一个 SQL 查询 (EF6) 中为多个数据库列选择可能的值

使用 Linq to Entities (EF6) 动态选择列名