gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:26:30 29 4
gpt4 key购买 nike

我是 Linq 和 EF Code First 的新手,遇到了一个我无法弄清楚的问题。我正在使用 ASP.Net Web Forms、EF Code First 和 Linq 开发概念验证检查程序。我的想法是我根据数据库动态生成页面的服务器控件。我以前在没有 EF 的情况下构建过相同的应用程序,但希望在这个项目中了解它。如果我使用文字但在数据绑定(bind) 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# 页面。请记住,我计划将数据访问分离到另一个类中,但只是想让一个演示为我自己工作。考试.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; }
}

问题.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; }
}

选择.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; }
}

测试.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";

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

关于c# - 我该如何解决这个 "System.Data.Entity.DynamicProxies"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28466424/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com