title: EF Core: Invalid column name 'companyId1'
body: <p>I have trouble understanding why a EF generated SELECT clause contains the primary key twice, the second one is postfixed with '1'.</p>
  
  <pre><code>exec sp_executesql N'SELECT [entity.WebAdminCompanyUser].[CompanyId], [entity.WebAdminCompanyUser].[AspNetUserId], [entity.WebAdminCompanyUser].[CompanyId1]
  FROM [SafeProtect].[WebAdminCompanyUser] AS [entity.WebAdminCompanyUser]
  INNER JOIN (
      SELECT [entity1].[AspNetUserId]
      FROM [SafeProtect].[WebAdminUser] AS [entity1]
      WHERE ([entity1].[RowEnabled] = 1) AND EXISTS (
          SELECT 1
          FROM [SafeProtect].[WebAdminCompanyUser] AS [companyUser1]
          WHERE ([companyUser1].[CompanyId] = @__companyId_0) AND ([entity1].[AspNetUserId] = [companyUser1].[AspNetUserId]))
  ) AS [t0] ON [entity.WebAdminCompanyUser].[AspNetUserId] = [t0].[AspNetUserId]
  ORDER BY [t0].[AspNetUserId]',N'@__companyId_0 int',@__companyId_0=1
  </code></pre>
  
  <p>It fails with <strong>Invalid column name 'CompanyId1'</strong>.</p>
  
  <p>Following are the entities and the corresponding configurations (fluent API):</p>
  
  <p><strong>WebAdminCompanyUser</strong>:</p>
  
  <pre><code>public partial class WebAdminCompanyUser : ITrackable, IMergeable
  {
      public WebAdminCompanyUser()
      {
          AdditionalInit();
      }
  
      public int CompanyId { get; set; }
      public int AspNetUserId { get; set; }
  
      public virtual Company Company { get; set; }
  
      [NotMapped]
      public TrackingState TrackingState { get; set; }
  
      [NotMapped]
      public ICollection&lt;string&gt; ModifiedProperties { get; set; }
  
      [NotMapped]
      public Guid EntityIdentifier { get; set; }
  
      partial void AdditionalInit();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Configuration:</p>
  
  <pre><code>builder.Entity&lt;WebAdminCompanyUser&gt;(entity =&gt;
  {
      entity.ToTable(name: "WebAdminCompanyUser", schema: SqlSchema.SafeProtect);
      entity.HasKey("CompanyId", "AspNetUserId");
  
      entity
         .HasOne(d =&gt; d.Company)
         .WithMany()
         .HasForeignKey(d =&gt; d.CompanyId)
         .IsRequired();
  });
  </code></pre>
  
  <p><strong>WebAdminUser</strong>:</p>
  
  <pre><code>public partial class WebAdminUser : IdentityUser&lt;int&gt;, IAuditInfo, IRowDisableableWithDateTime, ITrackable, IMergeable
  {
      public WebAdminUser()
      {
          WebAdminCompanyUser = new HashSet&lt;WebAdminCompanyUser&gt;();
          WebAdminUserRole = new HashSet&lt;WebAdminUserRole&gt;();
          WebAdminUserClaim = new HashSet&lt;WebAdminUserClaim&gt;();
          WebAdminUserLogin = new HashSet&lt;WebAdminUserLogin&gt;();
  
          AdditionalInit();
      }
  
      public string DisplayName { get; set; }
      public DateTime CreatedOn { get; set; }
      public DateTime? ModifiedOn { get; set; }
      public bool RowEnabled { get; set; }
      public DateTime? DisabledOn { get; set; }
  
      public virtual ICollection&lt;WebAdminCompanyUser&gt; WebAdminCompanyUser { get; set; }
      public virtual ICollection&lt;WebAdminUserRole&gt; WebAdminUserRole { get; set; }
      public virtual ICollection&lt;WebAdminUserClaim&gt; WebAdminUserClaim { get; set; }
      public virtual ICollection&lt;WebAdminUserLogin&gt; WebAdminUserLogin { get; set; }
  
      [NotMapped]
      public TrackingState TrackingState { get; set; }
  
      [NotMapped]
      public ICollection&lt;string&gt; ModifiedProperties { get; set; }
  
      [NotMapped]
      public Guid EntityIdentifier { get; set; }
  
      partial void AdditionalInit();
  }
  </code></pre>
  
  <p>Configuration:</p>
  
  <pre><code>    builder.Entity&lt;WebAdminUser&gt;(entity =&gt;
      {
          entity.ToTable(name: "WebAdminUser", schema: SqlSchema.SafeProtect);
          entity.Property(e =&gt; e.Id).HasColumnName("AspNetUserId");
  
          // authorize multiple user name
          entity.HasIndex((p) =&gt; new { p.UserName }).IsUnique(false);
  
          entity
                .HasMany(user =&gt; user.WebAdminUserClaim)
                .WithOne()
                .HasForeignKey(userClaims =&gt; userClaims.UserId)
                .IsRequired();
  
          entity
              .HasMany(user =&gt; user.WebAdminUserLogin)
              .WithOne()
              .HasForeignKey(userLogin =&gt; userLogin.UserId)
              .IsRequired();
  
          entity
              .HasMany(user =&gt; user.WebAdminUserRole)
              .WithOne()
              .HasForeignKey(userRole =&gt; userRole.UserId)
              .IsRequired();
  
          entity
               .HasMany(user =&gt; user.WebAdminCompanyUser)
               .WithOne()
               .HasForeignKey(companyUser =&gt; companyUser.AspNetUserId)
               .IsRequired();
      });
  </code></pre>
  
  <p>EF query:</p>
  
  <pre><code>IQueryable&lt;WebAdminUser&gt; query =
                      from WebAdminUser user WebAdminUserRepository.All()
                          .Include(user =&gt; user.WebAdminUserRole)
                              .ThenInclude(userRole =&gt; userRole.AspNetRole)
                          .Include(user =&gt; user.WebAdminCompanyUser)
                      where user.WebAdminCompanyUser.Any(companyUser =&gt; companyUser.CompanyId == companyId)
                      select user;
  
                  return query.ToList();
  </code></pre>
  
  <p>Any help appreciated.</p>