[ Using Code First DbContext with Entity Framework 5.0 RC ]
Not sure what is happening?
- the individual
Tracksare stored - and the individual
PlaybackEventsare stored - but the
ICollection<PlaybackEvent>ofTrackis not populated when I dovar tracks = myDbContext.Tracks.ToList(); - even though I saved with
myDbContext.Tracks.Add(track); myDbContext.SaveChanges();wheretrackhad a couple ofPlaybackEventsinICollection<PlaybackEvents>
Hopefully Relevant Parts of Track
public class Track
{
public string Id {get; set;}
private ICollection<PlaybackEvent> _playbackEvents;
public ICollection<PlaybackEvent> PlaybackEvents
{
get { return _playbackEvents ?? (_playbackEvents = new List<PlaybackEvent>()); }
set { _playbackEvents = value; }
}
}
Simplified PlaybackEvent
public class PlaybackEvent {
public string Id {get; set;}
public string Track_Id { get; set; }
}
Your
ICollection, which is your navigation property needs to be declared asvirtual.An additional suggestion to your code would be not to explicitly declare your getters and setters for your navigation property, but to move the setter responsibility to the constructor. Making your code look like this:
Your
PlaybackEvententity also needs a navigation property to enable the one Track to many PlaybackEvents relationship: