views:

51

answers:

1

Hello,

I have created my Database which has: Artist, tracks and TracksPerArtists

I can do sql queries through the entity model. For example when I make:

database.TRACK.ToList()

I get the list of track to be shown on index view for example. But my foreign keys come empty. While there is an artist and a track, and the correct row for ArtistsPerTrack, this item in my track.ToList() collection is empty.

Is there a different way to fetch those data?

I came from cakePHP framework in which you can define the Model.recursive property to declare the depth of the relations you want to fetch.

Is there anything similar here?

A: 

There is something similar. If "database" is DataContext and Artist and Tracks have many to many relationship in database, you can fetch related entities using Include clause:

database.TRACK.Include("Artists").ToList()

where "Artists" is the name of property on Track entity.

Misha N.
now my ViewData.Model has a non-null ArtistsPerTrack field but while ArtistsPerTrack.track is fulled with the track details, ArtistsPerTrack.Artist is null :(
gong
I'm not sure how your entities are associated, but maybe you should load ArtistsPerTrack and include Artists and Tracks: database.ArtistsPerTrack.Include("Artist").Include("Track").ToList(). Also you may call ArtistsPerTrack.ArtistReference.Load() before accessing property ArtistsPerTrack.Artist.
Misha N.
Your assumptions on my DB associations are correct! Nevertheless when I want to show the Track/Index page it's only natural to load the Track model, right?So if I do db.Artist.Include("artistPerTrack").ToList() you say I should gotten both Tracks and in every track the Artist information?
gong