我该如何解决这个 "System.Data.Entity.DynamicProxies" 错误

How can I solve this "System.Data.Entity.DynamicProxies" error

我是 Linq 和 EF Code First 的新手,遇到了一个我无法弄清楚的问题。我正在使用 ASP.Net Web Forms、EF Code First 和 Linq 开发概念验证检查程序。我的想法是我根据数据库动态生成页面的服务器控件。我以前在没有 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 知识,但如果有人能指出正确的方向,我将不胜感激。

这是我正在使用的三个 Class 文件和我正在尝试构建的 C# 页面。请记住,我计划将数据访问分开到另一个 class 但只是想为我自己制作一个演示。 Exam.cs

using 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) + ".&nbsp;" + 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();            
        }        
    }
}

我使用 ASP.NET WebForms 已经有一段时间了,但我认为你的问题出在这里:

ql.DataValueField = Convert.ToString(choice.ChoiceId);
ql.DataTextField = choice.ChoiceText;

我认为 Convert.ToString(choice.ChoiceId) 告诉控件使用名为“4”的 属性。

试试这个:

ql.DataValueField = "ChoiceId";
ql.DataTextField = "ChoiceText";

即您需要指定要使用的 属性 的名称,而不是值

尝试在您的项目 BbContext 构造函数中禁用 ProxyCreationEnabled:

Configuration.ProxyCreationEnabled = false;