Carsten Gehling
Carsten Gehling

Reputation: 1248

Entity framework data-first: Auto-load associated data

I am trying to use Entity Framework with an existing database. Using the code-first approach, I have gotten the following auto-created model (among others - I've tried to shorten the code to get to the essence of the question):

namespace Fidd.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Movies
    {
        public Movies()
        {
            ...
            this.MoviesPictures = new HashSet<MoviesPictures>();
            ...
        }

        public int MovieID { get; set; }
        public string MovieName { get; set; }
        ...

        public virtual ICollection<MoviesPictures> MoviesPictures { get; set; }
    }
}

So basically this is a 1-n relationship between Movies and MoviesPictures. I am still in the process of learning EF.

If I want to load a single Movie with

var movie = from m in dbContext.Movies
            where m.MovieID == 5
            select m;

How do I get the MoviesPictures collection to be loaded automatically? Either eager or lazy.

UPDATE: There is actually an extra association:

Movies 1..n MoviesPictures n..1 Pictures

The MoviesPictures model is defined like this:

public partial class MoviesPictures
{
    public int MoviePictureID { get; set; }
    public int MoviePictureMovieID { get; set; }
    public int MoviePicturePictureID { get; set; }
    public System.DateTime MoviePictureAddDatetime { get; set; }
    public bool MoviePictureRemoved { get; set; }
    public Nullable<System.DateTime> MoviePictureRemovedDatetime { get; set; }

    public virtual Movies Movies { get; set; }
    public virtual Pictures Pictures { get; set; }
}

Is there any way to eager load this 2. layer of association within the same query? I've tried to do this:

var model = from m in db.Movies.Include("MoviesPictures").Include("Pictures")
            where m.MovieID == id
            select m

which does not work - I get a runtime Exception that Pictures is not defined with an navigation-attribute in Movies. Which of course makes sense. I just don't know how to specify the query otherwise.

Another thing that worries me... The above Include() statement does not catch any errors during compile-time. Is there a way to specify this in a type-safe fashion?

/Carsten

Upvotes: 0

Views: 517

Answers (1)

CodingGorilla
CodingGorilla

Reputation: 19842

You would want to do:

var movie = from m in dbContext.Movies.Include("MoviesPictures")
            where m.MovieId == 5
            select m;

This is eagerly fetching the records from the "MoviesPictures" table. You can read a bit more about it here: http://msdn.microsoft.com/en-us/magazine/cc507640.aspx. Also if you google on "entity framework include" you can probably find a lot more information.

UPDATE

You might be able to do .Include("MoviesPictures.Pictures") it depends on how you have things set up. If not, then you want to do some joins; there's a good blog post here: http://weblogs.asp.net/salimfayad/archive/2008/07/09/linq-to-entities-join-queries.aspx on joins.

In regards to do it in "type safe"ly; this is the only method of "including" related records. You could, as I mentioned, use joining which might be a little closer to being "type safe".

Upvotes: 2

Related Questions