开发者

EF4 - Self tracking entities and inheritance and eager loading

I know this has been asked before in several ways but none of the answers seem applicable to me - or correct - or current, so I'll try again.

I have a large model with several intstances of inherited entities. One example is a Timetable that contains a collection of TimetableEvents. There are several sub-types of TimetableEvent, such as an InterviewTimetableEvent, BreakTimetableEvent and an ExercisetimeTableEvent. ExerciseTimetableEvent has a relationshi开发者_JS百科p to an Exercise entity.

I need to use self-tracking entities as I'm using a WCF back end to serve up data to several WPF clients in a stateless fashion.

So, I need to eager load everything and I thought that self-tracking entities would automatically do this but it appears they dont.

So, to get a timetable I need to do something like this:

            var tt = (from s in ESSDataContainer.Timetables
                   .Include("TimetableEvents")
                  where s.TimetableId == timetableid
                  select s).FirstOrDefault();

This will give me the TimetableEvents but not the Exercises that are related to the ExerciseTimetableEvents. Ive tried the following (and several other suggestions) without luck:

            var tt = (from s in ESSDataContainer.Timetables
                   .Include("TimetableEvents")
                   .Include("ExerciseTimetableEvents.Exercise")
                  where s.TimetableId == timetableid
                  select s).FirstOrDefault();

Is there a solution to this?

If not I'll go back to normal context tracking and connect to the database from a local container rather than using WCF.

Cheers


It's a bit tricky, but possible:

var tt = (from s in ESSDataContainer.Timetables
          where s.TimetableId == timetableid
          select new 
          {
              TimeTable = s,
              Events = s.TimeTableEvents,
              Exercise = s.TimeTableEvents.OfType<ExerciseTimetableEvents>()
                                          .Select(ett => ett.Exercise)
          }).Select(s => s.TimeTable)
            .AsEnumerable()
            .FirstOrDefault();

Clear as mud, but, hey: No magic strings! Also, it has the advantage that it actually works....


There is a Proposal for this issua at Microsoft Connect:. If you think this worthy you can vote for it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜