title: one to many relationship in entity framewrok 4.1 code first
body: <p>I am initializing the entities like this:</p>
  
  <pre><code>    TasksListDB db = new TasksListDB();
  
      public ActionResult Index()
      {
  
          var tasks1 = new List&lt;Task&gt;()
          {
              new Task { Id=1, Name = "Task 1", Difficulty = 1, DateCreated = DateTime.Parse("1/12/2011") , IsDone= true },
              new Task { Id=2, Name = "Task 2", Difficulty = 2, DateCreated = DateTime.Parse("11/2/2011") , IsDone = false}               
          };
  
          var tasks2 = new List&lt;Task&gt;()
          {
               new Task { Id=3, Name = "Task 3", Difficulty = 3, DateCreated = DateTime.Parse("11/2/2011") , IsDone = false},
              new Task { Id=4, Name = "Task 4", Difficulty = 5, DateCreated = DateTime.Parse("1/2/2010") , IsDone= true }
          };
  
          tasks1.ForEach(t =&gt; db.Tasks.Add(t));
          tasks2.ForEach(t =&gt; db.Tasks.Add(t));
  
          var Persons = new List&lt;Person&gt; { 
       new Person { Id= 1 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" },
       new Person { Id= 2 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed"  },
       new Person { Id= 3 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed"  },
       new Person { Id= 4 , Age= 10, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed"  }};
  
          Persons.ForEach(p =&gt; db.Persons.Add(p));
  
          return View(db.Tasks);
      }
  </code></pre>
  
  <p>my db context looks like this:</p>
  
  <pre><code>public class TasksListDB : DbContext
  {
      public DbSet&lt;Person&gt; Persons { get; set; }
      public DbSet&lt;Task&gt; Tasks { get; set; }   
  }
  </code></pre>
  
  <p>I want to assign tasks to persons how can I do this ?</p>
  
  <p>My model looks like this :</p>
  
  <pre><code>public class Person
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public int Age { get; set; }        
      public string EmailAddress { get; set; }        
      public virtual ICollection&lt;Task&gt; Tasks { get; set; }
  }
  
  public class Task
  {
      public int Id { get; set; }
      public string Name { get; set; }
      [Required]
      public DateTime DateCreated { get; set; }
      [Required]
      [Range(1,5,ErrorMessage="Difficulty must be between 1 and 5")]
      public int Difficulty { get; set; }
      public bool IsDone { get; set; }
  
      public virtual Person Person { get; set; }
  }
  </code></pre>
  
  <p>When I run my mvc application, Person and Task tables are created but juntion table PersonTask not created.</p>
  
  <p>[EDIT]</p>
  
  <pre><code>I am assigning tasks like this in initializer class:
  
     var t1 = new Task { Id = 1, Name = "Urgent Task", DateCreated = DateTime.Now, Difficulty = 2, IsDone = true };
              var t2 = new Task { Id = 2, Name = "Business Task", DateCreated = DateTime.Now, Difficulty = 1, IsDone = true };
              var t3 = new Task { Id = 3, Name = "Home Task", DateCreated = DateTime.Now, Difficulty = 2, IsDone = false };
  
   context.Tasks.Add(t1);
              context.Tasks.Add(t2);
              context.Tasks.Add(t3);
  
      var Persons = new List&lt;Person&gt; { 
           new Person { Id= 1 , Age= 40, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed", Tasks= new List&lt;Task&gt; { t1,t2 }},
           new Person { Id= 2 , Age= 30, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List&lt;Task&gt; { t1}  },
           new Person { Id= 3 , Age= 29, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List&lt;Task&gt; { t3,t2 } },
           new Person { Id= 4 , Age= 35, EmailAddress= "asif_hameed_37@hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List&lt;Task&gt; { t1,t3 } }};
  
   context.Persons.Add(Persons[0]);
              context.Persons.Add(Persons[1]);
              context.Persons.Add(Persons[2]);
              context.Persons.Add(Persons[3]);
  
              context.SaveChanges();
  
  but when i see the task table in sql server, it looks like this:
  
  Id  Name          DateCreated          Difficulty   IsDone  Person_Id
  1   Urgent Task 2011-04-24 19:32:03.990 2   1   4
  2   Business Task   2011-04-24 19:32:03.990 1   1   3
  3   Home Task   2011-04-24 19:32:03.990 2   0   4
  </code></pre>
  
  <p>I assume that person table should have tasks not the task table should have person Id</p>
  
  <pre><code>Please suggest solution.
  </code></pre>