I have a table structure in MySql, set up with foreign keys:
Period { Id, Title }
Activity { Id, Title, PeriodId }
Resource { Id, FileName }
ActivityResources { ActivityId, ResourceId }
This is set up with LINQ-to-Entities and I have checked that the table mappings are correct incuding the many-to-many relationship. Normally all works well with the Period
, Activity
and Resource
classes.
However, I am trying to use this code:
private string ListAttachments(Ctx ctx) {
var resList = new StringBuilder();
var p = ctx.Periods.Include("Activities.Resources").Single(o => o.Id == 1);
foreach (var a in p.Activities) foreach (var r in a.Resources)
resList.Append(r.Id).Append(" - ").Append(r.FileName);
return resList.ToString();
}
But it doesn't write the Resource.Id
for each resource as expected; for some reason it writes the Activity.Id
instead (though the FileName
is correct).
Any idea what's going on?
EDIT
By the way, this works fine - but I'm still interested in the probem in the above code.
private string ListAttachments(Ctx ctx) {
var resList = new StringBuilder();
var res = ctx.Resources.Where(r => r.LessonActivities.Any(l => l.LessonId == 1)).ToList();
foreach (var r in res) resList.Append(r.Id).Append(" - ").Append(r.FileName);
return resList.ToString();
}