title: EF Core code-first multiple foreign key relationships to same principal entity
body: <p>I started using EF Core with the database-first approach, I had two tables <code>CalendarDates</code> and <code>Deals</code>. A deal has multiple date-related properties for which separate foreign key relationships have been been set-up. In the below example <code>StartDate</code> and <code>EndDate</code>.</p>
  <p>The <code>scaffolded</code> DbContext showed the class definitions as such:</p>
  <pre class="lang-cs prettyprint-override"><code>    public class CalendarDate
      {
          public CalendarDate()
          {
              DealsStartedOnThatDate = new HashSet&lt;Deal&gt;();
              DealsEndedOnThatDate = new HashSet&lt;Deal&gt;();
          }
  
          [Key]
          public string Id { get; set; } = string.Empty;
          public DateTimeOffset Date { get; set; }
          public int Year { get; set; }
  
          public virtual ICollection&lt;Deal&gt; DealsStartedOnThatDate { get; set; }
          public virtual ICollection&lt;Deal&gt; DealsEndedOnThatDate { get; set; }
      }
  
      public class Deal
      {        
          [Key]
          public string Id { get; set; } = string.Empty;
          public string Name { get; set; } = string.Empty;
  
          public string StartDateId { get; set; } = string.Empty;
          public virtual CalendarDate StartDate { get; set; } = null!;
  
          public string EndDateId { get; set; } = string.Empty;
          public virtual CalendarDate EndDate { get; set; } = null!;
      }
  </code></pre>
  <p>I now want to make the move to code-first. But when I add the migration the following error is thrown:</p>
  <blockquote>
  <p>Unable to determine the relationship represented by navigation property 'CalendarDate.DealsStartedOnThatDate' of type 'ICollection&lt;Deal&gt;'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.</p>
  </blockquote>
  <p>If I comment-out the fields belonging to <code>EndDate</code> in both classes, it works just fine. So it seems that I cannot use more than one MANY:ONE relationship (many=Deals,one=CalendarDates) here. Althoug it worked just fine when scaffolded from an existing database with existing FK constraints.</p>
  <p>What am I missing here?</p>
  <p>Cheers</p>