title: How define properties as foreign keys in models to MVC Codetemplates generate it as FK
body: <p>I use EFCodefirst. Most of you know, but the following code is part of MVC3 T4 CodeTemplates in Edit.tt And also in Create.tt of AddView:</p>
  
  <pre><code> &lt;div class="editor-field"&gt;
   &lt;#
          if (property.IsForeignKey) {
   #&gt;
          @Html.DropDownList("&lt;#= property.Name #&gt;", String.Empty)
   &lt;#
          } else {
   #&gt;
          @Html.EditorFor(model =&gt; model.&lt;#= property.Name #&gt;)
   &lt;#
          }
   #&gt;
          @Html.ValidationMessageFor(model =&gt; model.&lt;#= property.Name #&gt;)
    &lt;/div&gt;
  </code></pre>
  
  <p>As you see if the CodeTemplates find a property as a FK generate a DropDownList automatically, but the question is how can define props as a FK? this is my models:</p>
  
  <pre><code>public class Artist {
  
      public long Id { get; set; }
      public string Name { get; set; }
      public Genre Genre { get; set; }
  }
  
  
  public class Genre {
      public long Id { get; set; }
      public string Title { get; set; }
      public ICollection&lt;Artist&gt; Artists { get; set; }
  
  }
  
  
  public class MusicGalleryDB : DbContext {
  
      public DbSet&lt;Artist&gt; Artists { get; set; }
      public DbSet&lt;Genre&gt; Genres { get; set; }
  
  }
  </code></pre>
  
  <p>I expected the Genre in Artist class be a FK and in create view of Artist I can select a Genre in dropdownlist, but the mvc don't generate any dropdownlist in views actually as mvc's view there is not any relation between Genre and Artist. whe I look at the Database tables I see the following:</p>
  
  <pre><code>Artist(Id, Name,Genre_Id)
  Genre(Id, Title)
  </code></pre>
  
  <p>as you see there is a column name Genre_Id in Artist table that is a FK to Id of Genre Table This table is generated automatically with EFCodefirst but the MVC CodeTemplate can not recognize FKs. </p>
  
  <p>How can I do this? </p>
  
  <p>I know I can write my own views but I am interesting to mvc generate views automatically in correct way. Does anyone know about this?</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>This is my controller (generated automatically by mvc):</p>
  
  <pre><code> public class ArtistController : Controller
  {
      private MusicGalleryDB db = new MusicGalleryDB();
  
      //
      // GET: /Artist/
  
      public ViewResult Index()
      {
          var artists = db.Artists.Include(a =&gt; a.Genre);
          return View(artists.ToList());
      }
  
      //
      // GET: /Artist/Details/5
  
      public ViewResult Details(int id)
      {
          Artist artist = db.Artists.Find(id);
          return View(artist);
      }
  
      //
      // GET: /Artist/Create
  
      public ActionResult Create()
      {
          ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title");
          return View();
      } 
  
      //
      // POST: /Artist/Create
  
      [HttpPost]
      public ActionResult Create(Artist artist)
      {
          if (ModelState.IsValid)
          {
              db.Artists.Add(artist);
              db.SaveChanges();
              return RedirectToAction("Index");  
          }
  
          ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title", artist.GenreID);
          return View(artist);
      }
  
      //
      // GET: /Artist/Edit/5
  
      public ActionResult Edit(int id)
      {
          Artist artist = db.Artists.Find(id);
          ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title", artist.GenreID);
          return View(artist);
      }
  
      //
      // POST: /Artist/Edit/5
  
      [HttpPost]
      public ActionResult Edit(Artist artist)
      {
          if (ModelState.IsValid)
          {
              db.Entry(artist).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          ViewBag.GenreID = new SelectList(db.Genres, "Id", "Title", artist.GenreID);
          return View(artist);
      }
  
      //
      // GET: /Artist/Delete/5
  
      public ActionResult Delete(int id)
      {
          Artist artist = db.Artists.Find(id);
          return View(artist);
      }
  
      //
      // POST: /Artist/Delete/5
  
      [HttpPost, ActionName("Delete")]
      public ActionResult DeleteConfirmed(int id)
      {            
          Artist artist = db.Artists.Find(id);
          db.Artists.Remove(artist);
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      protected override void Dispose(bool disposing)
      {
          db.Dispose();
          base.Dispose(disposing);
      }
  }
  </code></pre>