0

I'm doing an mvc Theater project with entity framework code first.
I created a page to add a movie to my database.
the Movie class has a Genre property,the Genre class has an Id and a Name property.
What I'm trying to do is query all rows of the Genres table from database with Linq (if other method is better do tell) and choose one Genre to connect to a row of the movie I'm creating.

thanks,any help is appreciated.

this is my Genre class

public class Genre
{
    [Key]
    public int Id { get; set; }
    [Display(Name="Ganre")]
    public string GenreName { get; set; }
    public virtual ICollection<Movie> MoviesByGenre { get; set; }
}

this is my Movie class

public class Movie
{
    [Key]
    public string Id { get; set; }
    [Required(ErrorMessage = "Movie name is required")]
    [Display(Name="Movie name")]
    public string MovieName { get; set; }
    [Required(ErrorMessage = "Movie length is required")]
    [Display(Name="Length(minutes)")]
    public int LengthInMinutes { get; set; }
    [Required(ErrorMessage="Genre of the movie is required")]
    [Display(Name="Genre")]
    public virtual Genre MovieGenre { get; set; }
    public string Description { get; set; }
    [Required(ErrorMessage = "Year of release is required")]
    public int Year { get; set; }
    [Required(ErrorMessage="Who is the director?")]
    public virtual MovieDirector Director { get; set; }
    public virtual MoviePoster Poster { get; set; }

    //How many users bought a ticket
    public virtual ICollection<ApplicationUser> UsersWhoBoughtTicket { get; set; }
}

this is the Add Movie page

<div class="form-horizontal">
        <h4>Movie</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.MovieName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.MovieName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.MovieName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LengthInMinutes, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LengthInMinutes, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LengthInMinutes, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Year, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Year, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Year, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.MovieGenre.GenreName, htmlAttributes: new { @class = "btn control-label col-md-2" })
            <div class="col-md-10">
                @Html.ValidationMessageFor(model => model.MovieGenre.GenreName, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Director, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Director, new { htmlAttributes = new { @class = "form-control dropdown" } })
                @Html.ValidationMessageFor(model => model.Director, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Poster, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="Image" type="file" />
                @Html.ValidationMessageFor(model => model.Poster)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>

1
  • can't it find on that article? if not can you put the code related to your issue? Commented Sep 8, 2016 at 10:52

1 Answer 1

1
I hope your entities are like this:

    public class Genre
    {
         public int Id{ get; set; }
         public string Name{ get; set; }

         public ICollection<Movie> Movies{ get; set; }
    }

    public class Movie
    {
         public int Id{ get; set; }
         public string Name{ get; set; }
         public int GenreId {get; set; }

         public Genre Genre{ get; set; }
    }

Now your query would be like this:
    _context.Include(x=>x.Movies).Genres; //it will return all genres with movies
    _context.Movies.where(x=>x.GenreId == genreId); // it will return movies based on a genre id
Sign up to request clarification or add additional context in comments.

3 Comments

I have edited my post. I'll appreciate if you'll take another look,thanks.
I couldn't figure out why you put this property: public int GenreId {get; set;} in which part of the code can I query from?
Hi @LiorAviel.. I added this GenreId property to make foreign key relationship between Movie and Genre entities. This is the proper way of creating fk relationships in entity framework. Also this GenreId is helpful to fetch records from Movies based on GenreId as i used it in my second query above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.