title: Add One to many relationship EF 6
body: <p>I'm working on an application for printers auditing. So, when an user choose a printer name, he could see all the users which are related to it. This is my database model :  </p>
  
  <p><a href="https://i.stack.imgur.com/vl648.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vl648.jpg" alt="enter image description here"></a></p>
  
  <p>I'm thinking about built a little script for sending user name account, pc name, list of printers and the datetime to a WCF service from user's PCs. Then a WCF service could upload these info in database. </p>
  
  <p>But... I have a problem to understand how I could create a new record with a one to many relationship.</p>
  
  <pre><code>public void AuditRecord(DateTime date,String user,string[] printer, String pc)
  {
  var USR = new Users ()
  {
      UserName = user,
  };
  
  var PC = new Pc()
  {
      PcName = pc,
  };
  
  var PTR = new Printers();
  List&lt;Printers&gt; ptr = new List&lt;Printers&gt;();
  for (int i = 0;i&lt;printer.Count;i++)
  {
      ptr.Add(new Printers
      {
          PrinterName= printer[i],
      });
  }
  
  var REF = new Reference()
  {
  Date = date,
  };
  
  context.SaveChanges(); }
  </code></pre>
  
  <p>EDIT :</p>
  
  <p>With this solution it's working for insertion.</p>
  
  <pre><code>ptr.ToList().ForEach(p=&gt;
  {
      var RF = new Reference() {Date=DateTime.Now};
      var USR = new Users() {UserName=user};
      var PC = new Pc() {PcName=pc};
      var PTR = new Printers() {PrinterName = p };
      PTR.Reference.Add(RF);
      USR.Reference.Add(RF);
      PC.Reference.Add(RF);
      context.Pc.Add(PC);
      context.Users.Add(USR);
      context.Printers.Add(PTR);
  });
  context.SavesChanges();
  </code></pre>
  
  <p>But I have a problem in reference table. If an user use three printers, I need to insert the Id's for each printer and the same Id's for User and Pc. Like this</p>
  
  <pre><code>Date     | PrinterId | UserId | PcId
  17/11/15        1         1       1
  17/11/15        2         1       1
  17/11/15        3         1       1
  </code></pre>
  
  <p>And I have :</p>
  
  <pre><code>Date     | PrinterId | UserId | PcId
  17/11/15        1         1       1
  17/11/15        2         2       2
  17/11/15        3         3       3
  </code></pre>