0

Im making a movieshop for school. I have a movie as a domain model, and each of these models has another model, Genre, as a property. Now when i save this to the database, it adds the movie and the genre to each of their tables, and it matches creates a genre id for det for movie and matches it with the genre table.

The problem is, that when i read the movie from the db again, the genre is null. it doesnt even have the genre id.

How do i assign the genre from the db to the movie?

public void Add(Movie mov)
    {

        using (Context mCon = new Context())
        {
            mCon.Movies.Add(mov);
            mCon.SaveChanges();
        }
    }

this^ is where i add the movie.

public List<Movie> GetAllMovies()
    {
        using (Context mCon = new Context())
        {
            return mCon.Movies.ToList();
        }
    }

and this is where i read it back from the db

EDIT*

Movie movie = new Movie() { Id = 1, Name = "Movie1", Price = 200d, Genre = new Genre() { Id = 1, Name = "Genre1" } };

EDIT** My context contains the dbset for genres and movies.

i add the movie i created to my facade:

facade.GetMovieRep().Add(movie);
Jonas Olesen
  • 520
  • 4
  • 18

1 Answers1

0

I found a solution

I just added .include("Genre") when i return the context to list

Jonas Olesen
  • 520
  • 4
  • 18
  • If you want to use lazy loading you can set the properties for Genre as virtual. This tells EF to include them as part of the result. – DDiVita Sep 22 '15 at 12:05