我对Linq和EF Code First还不太熟悉,遇到了一个问题,但是一直无法解决。我正在使用ASP.Net Web Forms、EF Code First和Linq开发一个概念验证考试程序。我的想法是根据数据库动态生成页面的服务器控件。在没有EF的情况下,我已经构建了相同的应用程序,但希望在这个项目中学习EF。如果我使用普通文本可以得到正确的结果,但在数据绑定RadioButtonList时出现错误。
DataBinding: 'System.Data.Entity.DynamicProxies.Choice_EA448AD48C19F54FBB6BF09B7A03BA899DBE75EC189635A8982E7C3B1D8F4ABD' does not contain a property with the name '4'.
Line 34: content.Controls.Add(ql);
Line 35: ql.DataSource = choices;
Line 36: ql.DataBind();
Line 37: }
“4”这个属性名只是我数据库中的一个值。我不确定这个问题是否与延迟加载有关,或者只是我在正确使用Linq方面缺乏知识,但如果有人能指导我正确的方向,我将不胜感激。
以下是我正在使用的三个类文件和我尝试构建的C#页面。请注意,我计划将数据访问分离到另一个类中,但只是想为自己制作一个演示。
Exam.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
/// <summary>
/// Summary description for Exam
/// </summary>
public class Exam
{
public int ExamId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool Status { get; set; }
public DateTime DateCreated { get; set; }
public string CreatedBy { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
Question.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Question
/// </summary>
public class Question
{
public int QuestionId { get; set; }
public string QuestionText { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public bool Status { get; set; }
public string CreatedBy { get; set; }
public string QuestionType { get; set; }
public int ExamId { get; set; }
public virtual Exam Exam { get; set; }
public virtual ICollection<Choice> Choices { get; set; }
}
Choice.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Choice
/// </summary>
public class Choice
{
public int ChoiceId { get; set; }
public string ChoiceText { get; set; }
public int Order { get; set; }
public bool Status { get; set; }
public bool Correct { get; set; }
public DateTime DateCreated { get; set; }
public string CreatedBy { get; set; }
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
}
Testing.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Testing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ExamsContext db = new ExamsContext();
var questions = (from c in db.Exams
from q in c.Questions
where c.ExamId == 1
select q).ToList();
ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("MainContent");
foreach (var question in questions)
{
Literal questionLabel = new Literal();
questionLabel.Text = Convert.ToString(question.QuestionId) + ". " + question.QuestionText + "<br/>";
content.Controls.Add(questionLabel);
var choices = (from c in question.Choices select c).ToList();
RadioButtonList ql = new RadioButtonList();
foreach (var choice in choices)
{
ql.DataValueField = Convert.ToString(choice.ChoiceId);
ql.DataTextField = choice.ChoiceText;
}
content.Controls.Add(ql);
ql.DataSource = choices;
ql.DataBind();
}
}
}