title: Many to many relationships MVC
body: <p>I created db from Microsoft Management Studio and generate classes in code with ADO, <a href="https://i.stack.imgur.com/juTQu.png" rel="nofollow noreferrer">Its my database schema</a>
  I have problem with m-m relation. I would like to get multiselect list in table <code>Samochody</code>, column: <code>Wyposazenie</code>, but I get null exception. Relation 1-n works good: <a href="https://i.stack.imgur.com/z4H2Y.png" rel="nofollow noreferrer">Relation 1-n</a>
  My class Wyposazenie.cs:</p>
  <pre><code>[Table(&quot;Wyposazenie&quot;)]
  public partial class Wyposazenie
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage(&quot;Microsoft.Usage&quot;, &quot;CA2214:DoNotCallOverridableMethodsInConstructors&quot;)]
      public Wyposazenie()
      {
          Samochody = new HashSet&lt;Samochody&gt;();
      }
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id_wyposazenia { get; set; }
  
      [Required]
      [StringLength(50)]
      public string Nazwa_wyposazenia { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage(&quot;Microsoft.Usage&quot;, &quot;CA2227:CollectionPropertiesShouldBeReadOnly&quot;)]
      public virtual ICollection&lt;Samochody&gt; Samochody { get; set; }
  }
  </code></pre>
  <p>Samochody.cs:</p>
  <pre><code>[Table(&quot;Samochody&quot;)]
  public partial class Samochody
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage(&quot;Microsoft.Usage&quot;, &quot;CA2214:DoNotCallOverridableMethodsInConstructors&quot;)]
      public Samochody()
      {
          Zdjecia = new HashSet&lt;Zdjecia&gt;();
          Wyposazenie1 = new HashSet&lt;Wyposazenie&gt;();
      }
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id_samochodu { get; set; }
  
      [Required]
      [StringLength(50)]
      public string Tytul { get; set; }
  
      [Required]
      [StringLength(50)]
      public string Podtytul { get; set; }
  
      public int? Rok { get; set; }
  
      public int? Przebieg { get; set; }
  
      public int? Pojemnosc { get; set; }
  
      [StringLength(200)]
      public string Rodzaj_paliwa { get; set; }
  
      [StringLength(200)]
      public string Kategoria { get; set; }
  
      [Required]
      [StringLength(200)]
      public string Marka { get; set; }
  
      [Required]
      [StringLength(200)]
      public string Model { get; set; }
  
      [StringLength(200)]
      public string Wersja { get; set; }
  
      [StringLength(200)]
      public string Generacja { get; set; }
  
      [StringLength(200)]
      public string Moc { get; set; }
  
      [StringLength(200)]
      public string Skrzynia_biegow { get; set; }
  
      [StringLength(200)]
      public string Naped { get; set; }
  
      [StringLength(200)]
      public string Vin { get; set; }
  
      [StringLength(200)]
      public string Emisja_co2 { get; set; }
  
      [StringLength(200)]
      public string Typ { get; set; }
  
      public int? Liczba_drzwi { get; set; }
  
      public int? Liczba_miejsc { get; set; }
  
      [StringLength(200)]
      public string Kolor { get; set; }
  
      [Column(TypeName = &quot;date&quot;)]
      public DateTime? Pierwsza_rejestracja { get; set; }
  
      [StringLength(200)]
      public string Numer_rejestracyjny { get; set; }
  
      [StringLength(200)]
      public string Zarejestrowany_w_polsce { get; set; }
  
      [StringLength(200)]
      public string Stan { get; set; }
  
      [StringLength(200)]
      public string Kraj_pochodzenia { get; set; }
  
      [StringLength(200)]
      public string Opis { get; set; }
  
      [StringLength(200)]
      public string Wyposazenie { get; set; }
  
      public int? Kwota { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage(&quot;Microsoft.Usage&quot;, &quot;CA2227:CollectionPropertiesShouldBeReadOnly&quot;)]
      public virtual ICollection&lt;Zdjecia&gt; Zdjecia { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage(&quot;Microsoft.Usage&quot;, &quot;CA2227:CollectionPropertiesShouldBeReadOnly&quot;)]
      public virtual ICollection&lt;Wyposazenie&gt; Wyposazenie1 { get; set; }
  }
  </code></pre>
  <p>SamochodyController.cs:</p>
  <pre><code>   [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include = &quot;Id_samochodu,Tytul,Podtytul,Rok,Przebieg,Pojemnosc,Rodzaj_paliwa,Kategoria,Marka,Model,Wersja,Generacja,Moc,Skrzynia_biegow,Naped,Vin,Emisja_co2,Typ,Liczba_drzwi,Liczba_miejsc,Kolor,Pierwsza_rejestracja,Numer_rejestracyjny,Zarejestrowany_w_polsce,Stan,Kraj_pochodzenia,Opis,Wyposazenie,Kwota&quot;)] Samochody samochody)
      {
          if (ModelState.IsValid)
          {
              db.Samochody.Add(samochody);
              db.SaveChanges();
              return RedirectToAction(&quot;Index&quot;);
          }
  
          return View(samochody);
      }
  </code></pre>
  <p>My Samochody Create View:</p>
  <pre><code>  &lt;div class=&quot;form-group&quot;&gt;
          @Html.LabelFor(model =&gt; model.Wyposazenie, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
          &lt;div class=&quot;col-md-10&quot;&gt;
            @Html.DropDownListFor(m =&gt; m.Wyposazenie, new SelectList(Model.Wyposazenie1, &quot;Nazwa_wyposazenia&quot;), &quot;Choose&quot;, new { @class = &quot;form - control&quot; })
  
  
        
            @Html.ValidationMessageFor(model =&gt; model.Wyposazenie, &quot;&quot;, new { @class = &quot;text-danger&quot; })
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>