title: Autofill navigation Id property to child class when inserting into database with entity framework
body: <p>Let's say I have User and CreditCard classes in online shop application:</p>
  
  <pre><code>User() {
    int Id;
    List&lt;CreditCard&gt;() CreditCards;
  }
  
  CreditCard() {
    int Id;
    int UserId:
    virtual User User;
  }
  </code></pre>
  
  <p>User needs at least one CreditCard, and CreditCard requires User.
  At this moment I'm using following entity type configuration:</p>
  
  <p>For User:</p>
  
  <pre><code> HasMany(x =&gt; x.CreditCards)
     .WithRequired(x =&gt; x.User)
     .HasForeignKey(x =&gt; x.UserId);
  </code></pre>
  
  <p>For CreditCard:</p>
  
  <pre><code> HasRequired(x =&gt; x.User)
     .WithRequired(x =&gt; x.CreditCards)
     .HasForeignKey(x =&gt; x.UserId);
  </code></pre>
  
  <p>At this moment everything is working fine, but I want to add a way to specify main CreditCard like so:</p>
  
  <pre><code>User() {
    int Id;
    int MainCreditCardId;
    List&lt;CreditCard&gt;() CreditCards;
  }
  </code></pre>
  
  <p>MainCreditCardId is pointing to one of cards from the list.</p>
  
  <p>The problem begins with posting new User to the system - Main credit card is required, but i don't have it's id yet, since it is filled by database itentity. What would be the way to assign this id to MainCreditCardId?</p>
  
  <p>POST model would look like this:</p>
  
  <pre><code>User() {
    int Id;
    CreditCard MainCreditCard;
    List&lt;CreditCard&gt;() CreditCards;
  }
  </code></pre>
  
  <p>And GET one like this:</p>
  
  <pre><code>User() {
    int Id;
    int MainCreditCardId;
    List&lt;CreditCard&gt;() CreditCards;
  }
  </code></pre>
  
  <p>Of course, the models are simplified for the needs of the question</p>