title: EF Multiple added entities may have the same primary key error
body: <p>I'm going to create a new cascade <strong>[node] --1:many-- [currency] --1:many-- [unit]</strong> defined using EF 6.x Code First:</p>
  
  <pre><code>public class CashFlowSpendableMinimumNode {
    ...
    [ForeignKey("CashFlowSpendableMinimumNodeId")]
    public ICollection&lt;CashFlowSpendableMinimumCurrency&gt; CashFlowSpendableMinimumCurrencies { get; private set; }
    ...
  }
  public class CashFlowSpendableMinimumCurrency {
    ...
    public int CashFlowSpendableMinimumNodeId { get; set; }
    [ForeignKey("CashFlowSpendableMinimumNodeId")]
    public CashFlowSpendableMinimumNode CashFlowSpendableMinimumNode { get; set; 
    ...
    [ForeignKey("CashFlowSpendableMinimumCurrencyId")]
    public ICollection&lt;CashFlowSpendableMinimumUnit&gt; CashFlowSpendableMinimumUnits { get; set; }
    ...
  }
  
  public class CashFlowSpendableMinimumUnit {
    ...
    public int CashFlowSpendableMinimumCurrencyId { get; set; }
    [ForeignKey("CashFlowSpendableMinimumCurrencyId")]
    public CashFlowSpendableMinimumCurrency CashFlowSpendableMinimumCurrency { get; set; }
    ...
  }
  </code></pre>
  
  <p>The first part i.e. relation between <strong>node--currency</strong> work as expected: I create a new currency instance and add it to <em>DbSet&lt;CashFlowSpendableMinimumCurrency&gt;</em> -> it appears in the navigation property (collection) <em>nodeMinimum.CashFlowSpendableMinimumCurrencies</em> and its Id is resolved properly when relation is saved to DB.</p>
  
  <p>The second relation <strong>currency--unit</strong> does not work even if I tried to define it the same way as the first relation between <strong>node--currency</strong>: when I create a new unit instance and add it to <em>DbSet&lt;CashFlowSpendableMinimumUnit&gt;</em> it <strong>does not appear</strong> in the navigation property <em>currMinimum.CashFlowSpendableMinimumUnits</em> and if I continue execution, an attempt to make cascade persistent by calling <em>SaveChanges()</em> later results in the error in the %subject%.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I omit exact wording of error message, here it is:</p>
  
  <p><em>Unable to determine the principal end of the 'xxx.yyy.Database.CashFlowSpendableMinimumNode_CashFlowSpendableMinimumCurrencies' relationship. Multiple added entities may have the same primary key.</em></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>The only solution to make it running I've discovered is to flush the change tracker prematurely:</p>
  
  <pre><code>foreach (var node in nodes)
  {
     nodeMinimum = new ...
     DbSet&lt;CashFlowSpendableMinimumNode&gt;.Add(nodeMinimum);
     foreach (var curr in currencies)
     {
        currMinimum = new ...
        DbSet&lt;CashFlowSpendableMinimumCurrency&gt;.Add(currMinimum);
  
        DbContext.SaveChanges();  //The premature flush to fix a problem
  
        foreach (var unit in units)
        {
           unitMinimum = new ...
           DbSet&lt;CashFlowSpendableMinimumUnit&gt;.Add(unitMinimum);
        }
     }
  }
  DbContext.SaveChanges();
  ...
  </code></pre>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>Concerning discused constructors and instance creation:</p>
  
  <pre><code>public CashFlowSpendableMinimumCurrency()
  {
    CashFlowSpendableMinimumUnits = new List&lt;CashFlowSpendableMinimumUnit&gt;();
  }
  
  [...]
  CashFlowSpendableMinimumCurrency result = new CashFlowSpendableMinimumCurrency()
  {
    CurrencyCode = ACurrencyCode,  // the attached entity
    Curr = ACurrencyCode.Curr,     // string
    Minimum = minimum,  //decimal
    Median = median,  //decimal
    CashFlowSpendableMinimumNode = ANodeMinimum,  // parent in 1:n relation, EntityState.Added
    ExchangeRate = 1, //decimal
    DeclaredExchangeRate = false,
    BanknotesContribution = 0, //double
    BanknotesSpendableMinimum = 0, //decimal
    CoinsContribution = 0, //double
    CoinsSpendableMinimum = 0, //decimal
  };
  ADbContext.CashFlowSpendableMinimumCurrencies.Add(result);
  </code></pre>
  
  <p>Please, does anybody see, what is wrong?</p>
  
  <p>Thanks, pf</p>