title: MCV3 Entity Framework 4 Code First: Adding Sub Objects to the db
body: <p>I am trying to teach my self MVC3 and EF4 using code first and the DbContext generator, so forgive me if this is a silly question.</p>
  
  <p>Basically I have a user class, and an email class; this is because i want each user to be able to have multiple email addresses. the classes are set up like this:</p>
  
  <pre><code>public class User
  {
      [Key]    
      public int Id { get; set; }    
      public string User_Name { get; set; }
      public string Password { get; set; }
      public string First_Name { get; set; }
      public string Last_Name { get; set; }
  
      public virtual ICollection&lt;Email&gt; Emails { get; set; }
  }
  
  public class Email
  {
      [Key]
      public int Id { get; set; }
      public string Address { get; set; }
  
      public User User { get; set; }        
  }
  </code></pre>
  
  <p>I am happily manipulating the user class using the CRUD methods build by MVC3 and inserting users programmatically to "seed" the db with test data, the latter i am doing like so by overriding the <code>Seed</code> method in the <code>DropCreateDatabaseAlways</code> class like so:</p>
  
  <pre><code>public class dbInitializer : DropCreateDatabaseAlways&lt;UserContext&gt;
  {
      protected override void Seed(UserContext context)
      {
          var Users = new List&lt;User&gt;
          {
              new User {  User_Name = "uname",
                          Password = "pword",
                          First_Name = "fname",
                          Last_Name = "sname",
              }
          };
  
          Users.ForEach(u =&gt; context.Users.Add(u));
      }
  }
  </code></pre>
  
  <p>Now i would also like to add so email addresses, and because of the way i set up my classes code first obviously realises that each user can have multiple email addresses and each email addresses can belong only to one user because when creating a new user or email object the intellisense (VS10) presents me with <code>Emails</code> and <code>User</code> properties that are not actually part of either class.</p>
  
  <p>My question is this: How do i add an email address to a user as its created, and how do i add an email address to a user that has been created previously?</p>