繁体   English   中英

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

[英]FormatException when using multiple Include() methods in MySQL Linq Query EF6

我正在尝试执行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并且没有null。

是什么导致此错误?

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

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();

我设法通过切换包含顺序并将错误的包含放在首位来使我的代码正常工作(在您的情况下为x.QuizPasswords)

我找到了一种强制将属性设置为“位”默认值“ false”的解决方案。 我在Mysql中更改了数据类型。 Tinyint->位。

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

链接参考:

实体框架MySQL tinyint(1)System.Boolean.Parse FormatException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM