title: How can I use EF to add multiple child entities to an object when the child has an identity key?
body: <p>We are using EF5 and SQL Server 2012 the following two classes:</p>
  
  <pre><code>public class Question
  {
      public Question()
      {
          this.Answers = new List&lt;Answer&gt;();
      }
      public int QuestionId { get; set; }
      public string Title { get; set; }
      public virtual ICollection&lt;Answer&gt; Answers { get; set; }
  
  }
  public class Answer
  {
      public int AnswerId { get; set; }
      public string Text { get; set; }
      public int QuestionId { get; set; }
      public virtual Question Question { get; set; }
  }
  </code></pre>
  
  <p>Mapping is as follows:</p>
  
  <pre><code>public class AnswerMap : EntityTypeConfiguration&lt;Answer&gt;
  {
      public AnswerMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.AnswerId);
  
          // Identity
          this.Property(t =&gt; t.AnswerId)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  </code></pre>
  
  <p>Database DDL</p>
  
  <pre><code>CREATE TABLE Answer (
      [AnswerId] INT IDENTITY (1, 1) NOT NULL,
      [QuestionId] INT NOT NULL,
      [Text] NVARCHAR(1000),
      CONSTRAINT [PK_Answer] PRIMARY KEY CLUSTERED ([AnswerId] ASC)
  )";
  </code></pre>
  
  <p>Here are the results of what I have tried:</p>
  
  <p><strong>This works for one child:</strong></p>
  
  <pre><code>var a = new Answer{
      Text = "AA",
      QuestionId = 14
  };
  question.Answers.Add(a);
  _uow.Questions.Update(question);
  _uow.Commit();
  </code></pre>
  
  <p><strong>This does not work for more than one child:</strong></p>
  
  <p>Error: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.</p>
  
  <pre><code>var a = new Answer{
      AnswerId = 0,
      Text = "AAA",
      QuestionId = 14
  };
  var b = new Answer {
      AnswerId = 0,
      Text = "BBB",
      QuestionId = 14
  };
  question.Answers.Add(a);
  question.Answers.Add(b);
  _uow.Questions.Update(question);
  _uow.Commit();
  </code></pre>
  
  <p><strong>This does not work for more than one child:</strong></p>
  
  <p>It creates AnswerID's 1000 and 1001 but I want new Id's to be created by the database.</p>
  
  <pre><code>var a = new Answer{
      AnswerId = 1000,
      Text = "AAA",
      QuestionId = 14
  };
  var b = new Answer {
      AnswerId = 1001,
      Text = "BBB",
      QuestionId = 14
  };
  question.Answers.Add(a);
  question.Answers.Add(b);
  _uow.Questions.Update(question);
  _uow.Commit();
  </code></pre>
  
  <p><strong>Does not work:</strong> </p>
  
  <p>Compiler error. Can't convert null to int</p>
  
  <pre><code>var a = new Answer{
      AnswerId = null,
      Text = "AAA",
      QuestionId = 14    
  };
  var b = new Answer
  {
      AnswerId = null,
      Text = "BBB",
      QuestionId = 14
  };
  question.Answers.Add(a);
  question.Answers.Add(b);
  _uow.Questions.Update(question);
  _uow.Commit();
  </code></pre>
  
  <p><strong>Doesn't work:</strong> </p>
  
  <p>ObjectStateManager cannot track multiple objects with the same key.</p>
  
  <pre><code>var a = new Answer{
      Text = "AAA",
      QuestionId = 14
  };
  var b = new Answer
  {
      Text = "BBB",
      QuestionId = 14
  };
  question.Answers.Add(a);
  question.Answers.Add(b);
  _uow.Questions.Update(question);
  _uow.Commit();
  </code></pre>
  
  <p>In my application I have one or more new Answer objects generated on the client and then these are sent to the server. Above <strong>I am simulating what will happen</strong> without adding the client into the question. Note that the adding of all Answers to the Question object is done on the client and then comes over in a JSON string to the server. It is then deserialized to a Question Object like this:</p>
  
  <pre><code>public HttpResponseMessage PutQuestion(int id, Question question) {
              _uow.Questions.Update(question);
              _uow.Commit();
  </code></pre>
  
  <p>I want each Answer objects to be created with a <code>new identity ID</code>, for these to be added to the Question object and for the Question object to be returned back in the normal way.</p>
  
  <p>I don't know how this can be done. All my simple tests so far don't work. Please note this is a variation on an earlier question by our group member which was less clear and which I am trying to close. This question is I hope more clear.</p>
  
  <p>Notes:</p>
  
  <p>Here is the way update is coded:</p>
  
  <pre><code>public virtual void Update(T entity)
  {
      DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
      if (dbEntityEntry.State == EntityState.Detached)
      {
          DbSet.Attach(entity);
      }  
      dbEntityEntry.State = EntityState.Modified;
  }
  </code></pre>