我正在尝试执行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)
类型并设置为1
或0
且无空值。
可能导致此错误的原因是什么?
答案 0 :(得分:2)
我遇到了同样的问题并在第一个错误示例中使用了此解决方法:
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();
答案 1 :(得分:1)
我已经设法通过切换包含顺序并将错误的包含放在首位(在您的情况下是x.QuizPasswords)来使我的代码工作
答案 2 :(得分:0)
我找到了一种强制将属性设置为“位”默认值“假”的解决方案。我在Mysql中更改了数据类型。 Tinyint->位。
[Column("Active", TypeName = "bit")]
[DefaultValue(false)]
public bool Active { get; set;
链接参考:
Entity Framework MySQL tinyint(1) System.Boolean.Parse FormatException