我如何解决这个“System.Data.Entity.DynamicProxies”错误 [英] How can I solve this "System.Data.Entity.DynamicProxies" error

查看:1211
本文介绍了我如何解决这个“System.Data.Entity.DynamicProxies”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linq和EF Code First的新手,遇到了一个我根本无法想象的问题。我正在使用ASP.Net Web Forms,EF Code First和Linq开发概念验证程序。想法是我基于数据库动态生成页面的服务器控件。在没有EF的情况下,我已经构建了同样的应用程序,但希望能够了解到这个项目。我可以得到正确的结果,如果我使用文字,但收到一个错误,而数据绑定一个RadioButtonList。

  DataBinding:'系统。 Data.Entity.DynamicProxies.Choice_EA448AD48C19F54FBB6BF09B7A03BA899DBE75E​​C189635A8982E7C3B1D8F4ABD'不包含名称为4的属性。 

第34行:content.Controls.Add(ql);
第35行:ql.DataSource =选择;
第36行:ql.DataBind();
行37:}

名称为4的属性只是一个值从我的数据库。我不知道这个问题是否与Lazy Loading有关,或者只是缺乏适当的Linq知识,但如果有人能指出我正确的方向,我会非常感激。



这是我正在使用的三个Class文件以及我正在尝试构建的C#页面。请记住,我打算将数据访问分成另一个类,但只是试图为自己做一个演示。
Exam.cs

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.ComponentModel.DataAnnotations;

///< summary>
/// Exam的简要说明
///< / summary>
public class Exam
{
public int ExamId {get;组; }

public string标题{get;组; }

public string描述{get;组; }

public bool Status {get;组; }

public DateTime DateCreated {get;组; }

public string CreatedBy {get;组; }

public virtual ICollection< Question>问题{get;组; }
}

Question.cs

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

///< summary>
/// Question
的总结说明///< / summary>
public class Question
{
public int QuestionId {get;组; }

public string QuestionText {get;组; }

public int Order {get;组; }

public DateTime DateCreated {get;组; }

public bool Status {get;组; }

public string CreatedBy {get;组; }

public string QuestionType {get;组; }

public int ExamId {get;组; }

public virtual考试考试{get;组; }

public virtual ICollection< Choice>选择{get;组; }
}

Choice.cs

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;

///< summary>
/// Choice的摘要说明
///< / summary>
public class Choice
{
public int ChoiceId {get;组; }

public string ChoiceText {get;组; }

public int Order {get;组; }

public bool Status {get;组; }

public bool Correct {get;组; }

public DateTime DateCreated {get;组; }

public string CreatedBy {get;组; }

public int QuestionId {get;组; }

public virtual问题问题{get;组; }
}

Testing.aspx.cs

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;

public partial class测试:System.Web.UI.Page
{

protected void Page_Load(object sender,EventArgs e)
{
ExamsContext db = new ExamsContext();

var questions =(from c in db.Exams
from c in c.Questions
其中c.ExamId == 1
select q).ToList() ;
ContentPlaceHolder content =(ContentPlaceHolder)this.Master.FindControl(MainContent);
foreach(问题中的var问题)
{
文字问题Label = 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 =选择;
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;

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


I am new to Linq and EF Code First and came across an issue that I just can't figure out. I am developing a proof of concept examination program using ASP.Net Web Forms, EF Code First and Linq. The ideas is that I generate the page's server controls dynamically based on the database. I've built this same application before without EF but was hoping to learn that on this project. I can get the correct results if I use a literal but am receiving an error while databinding a 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:         }

The property with the name of '4' is just a value from my database. I'm not sure if this problem is related to Lazy Loading or just lack of knowledge in proper Linq but I would really appreciate it if someone can point me in the right direction.

Here are my three Class files I am using and the C# page that I am trying to build. Keep in mind I plan to separate the data access into another class but was just trying to get a demo working for myself. 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();            
        }        
    }
}

解决方案

It's a while since I used ASP.NET WebForms, but I think your problem is here:

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

I think that Convert.ToString(choice.ChoiceId) is telling the control to use a property named "4".

Try this:

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

i.e. you need to specify the name of the property to use, not the value

这篇关于我如何解决这个“System.Data.Entity.DynamicProxies”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆