I have the following classes and I am having a problem with getting data from them:
public partial class Exam {
public Exam()
{
this.Objectives = new List<Objective>();
}
public int ExamId { get; set; }
public int SubjectId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Objective> Objectives { get; set; }
}
public partial class Objective {
public int ObjectiveId { get; set; }
public int ExamId { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public virtual Exam Exam { get; set; }
}
I am getting a list of Objectives and I want to include Exam.Name.
Here is the query that I created so I could get Exam which would give me a way to get the Exam name.
public IList<Objective> GetObjectives(int examId)
{
var objectives = _objectivesRepository
.GetAll()
.Include(o => o.Exam)
.ToList();
return objectives;
}
Here is the mapping that I am using:
public ObjectiveMap()
{
this.HasRequired(t => t.Exam)
.WithMany(t => t.Objectives)
.HasForeignKey(d => d.ExamId)
.WillCascadeOnDelete(false);
}
Unfortunately this small query returns over 6MB of data. When I check with Fiddler I see:
Objective objects > Exam objects > Objective objects
What I need to have is:
Objective objects > Exam objects
Is there a solution for this. How can I stop EF5 from getting another layer of objectives?
JSOn serialization is a different issue. Not related to entity framework here. You can use ScriptIgnore
attribute to avoid serializing it,
public partial class Exam {
//....
[ScriptIgnore]
public virtual ICollection<Objective> Objectives { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.