2016-09-08 4 views
0

私はmvcエンティティフレームワークコードを最初に使ったシアタープロジェクトを作成しています。
私は自分のデータベースにムービーを追加するためのページを作成しました。
ムービークラスは、ジャンルプロパティ、ジャンルクラスには、IdプロパティとNameプロパティがあります。
(他の方法は教えてくれない方が良い場合)私は何をしようとしていることは、クエリLinqでデータベースからジャンルのテーブルのすべての行でと私が作成しています映画の行に接続するために、1 ジャンルを選択。データベースからのlinqを使用したクエリ

おかげで、任意のヘルプは大歓迎です。

これは私のジャンルクラス

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

である、これは私のMovieクラス

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; } 
} 

あるこのムービーページに追加

<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>

+0

その記事で見つけることができません?あなたの問題に関連するコードを置くことができないのですか? – Sampath

答えて

1
です
+0

投稿を編集しました。もう一度見ていただければ幸いです。 –

+0

私はこのプロパティを置く理由を理解できませんでした:public int GenreId {get; set;}コードの一部をどこからクエリできますか? –

+0

こんにちは@LiorAviel ..このGenreIdプロパティを追加して、ムービーエンティティとジャンルエンティティとの間に外部キー関係を作成しました。これは、エンティティフレームワークでfk関係を作成する適切な方法です。また、このGenreIdはGenreIdに基づくムービーから上記の2番目のクエリで使用したレコードをフェッチするのに役立ちます。 –

関連する問題