简体   繁体   English

当我在EF5中包含父级数据时,是否有办法避免获取子级数据的另一个副本?

[英]Is there a way I can avoid getting another copy of a child data when I include a parent in EF5?

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. 我正在获取目标列表,并且要包含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. 不幸的是,这个小查询返回了6MB以上的数据。 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? 如何阻止EF5获得另一层目标?

JSOn serialization is a different issue. JSOn序列化是另一个问题。 Not related to entity framework here. 这里与实体框架无关。 You can use ScriptIgnore attribute to avoid serializing it, 您可以使用ScriptIgnore属性来避免对其进行序列化,

public partial class Exam    {
 //....
    [ScriptIgnore]
    public virtual ICollection<Objective> Objectives { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM