title: EF HasOptional WithMany
body: <p>Im having a problem that i just cant solve by google or searching on stackoverflow.</p>
  <p>i have two tables JournalLines and Accounts and i want to reference a account in a journalline (i hate economics) but the reference has to be optional and Account shoulnt have any navigation property to the JournalLine (ideally).</p>
  <p><strong>JournalLine:</strong></p>
  <pre><code>public class JournalLine : Entity&lt;int&gt;
  {
      public int? Account_Id { get; set; }
      public string Desc { get; set; }
      public decimal Credit { get; set; }
      public decimal Debit { get; set; }
      public virtual Account Account { get; set; }
  }
  </code></pre>
  <p><strong>_Account:</strong></p>
  <pre><code>public class Account : Entity&lt;int&gt;
  {
      public string Accid { get; set; }
      public string Desc { get; set; }
      public int VatcodeId { get; set; }
  }
  </code></pre>
  <p><strong>And the respective mappings:</strong></p>
  <pre><code>public class JournalLineMap : EntityTypeConfiguration&lt;JournalLine&gt;
  {
      public JournalLineMap()
      {
          HasKey(k =&gt; new { k.Id, k.Account_Id });
          Property(k =&gt; k.Id)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          
          ToTable(&quot;attJournalLines&quot;);
          HasEntitySetName(&quot;JournalLines&quot;);
          
          HasOptional(jl =&gt; jl.Account)
              .WithMany()
              .HasForeignKey(jl =&gt; jl.Account_Id);
      }
  }
  
  public class AccountMap : EntityTypeConfiguration&lt;Account&gt;
  {
      public AccountMap()
      {
          ToTable(&quot;attAccounts&quot;);
          HasEntitySetName(&quot;Accounts&quot;);
      }
  }
  </code></pre>
  <p>The error im getting is like:</p>
  <blockquote>
  <p>One or more validation errors were detected during model generation:</p>
  <p>tSystem.Data.Entity.Edm.EdmAssociationType: : Multiplicity conflicts with the referential &gt;constraint in Role 'JournalLine_Account_Target' in relationship 'JournalLine_Account'. &gt;Because all of the properties in the Dependent Role are non-nullable, multiplicity of the &gt;Principal Role must be '1'.</p>
  </blockquote>
  <p>This confuses me and i hope some one can shed som light on the problem.</p>
  <h1>Update</h1>
  <p>Thanks for the answers it helped me on the way and i got the relation to work by removing the key. Then i got a wierd behavoir when assigning an <code>Account</code> to a <code>JournalLine</code> a duplicate account was created. As it turns out this isnt such weird behaviour since im using a Repository pattern with dependency injection. What i didnt think of was that the context wasnt the same in both repositories so the JournalLinesRepository wasnt tracking the account i fetched from its own repository and therefor thought it smart to just insert it as new entity. This was solved by injecting the same context in the repositories so the item tracking works as expected. Again thankyou for your help.</p>