title: Entity Framework 7 : Generating Invalid Column Name
body: <p>using EF 7 : 1.0.0-rc1-final,</p>
  
  <p>I'm having trouble with EF generating the query properly, using database first approach - using ef scaffolding to to generate some model properties listed in the DbContext - as the tables contain a large number of columns I only need a few to work for the webapi so they are column mapped</p>
  
  <p>I have 3 entities, Brands, Events and Sessions</p>
  
  <p>Brands contains many Events and Events contains many Sessions</p>
  
  <p>my models:</p>
  
  <pre><code>[Table("tblBranding")]
  public class Brand 
  {
      [Key]
      [Column("brandingId")]
      public int BrandId { get; set; }
      [Column("BrandingActive")]
      public bool Active { get; set; }
      [JsonIgnore]
      [Column("DeadBrand")]        
      public bool DeadBrand { get; set; }
      [Column("BrandingSiteTitle")]
      public string Name { get; set; }
  
      //navigation properties
      public virtual ICollection&lt;Event&gt; Events { get; set; }
  }
  
  [Table("tblEvents")]
  public class Event
  {           
      public int EventId { get; set; }
      [Column("eventActive")]
      public bool Active { get; set; }
      [Column("eventName")]
      public string Name { get; set; }        
      public DateTime EventCloseDate {get;set;}        
      public int PaxAllocationLimit { get; set; }
  
      //navigation properties             
      [JsonIgnore]        
      [Column("brandingId")]
  
      public virtual int BrandId { get; set; }
      [JsonIgnore]
      [ForeignKey("BrandId")]
      public virtual Brand Brand { get; set; }
      public virtual ICollection&lt;Session&gt; Sessions { get; set; }
  }
  
  [Table("tblEventsDates")]
  public class Session
  {       
      [Column("EventDateID")]
      public int SessionId { get; set; }
      [Column("EventDateName")]
      public string Name { get; set; }               
      [Column("EventDate")]
      public DateTime SessionDate { get; set; }        
      [Column("EventDateTime")]
      public DateTime SessionTime { get; set; }
      [Column("EventDateMinutes")]
      public decimal? SessionDurationInMinutes { get; set; }
      [Column("EventDateArrival")]
      public DateTime? ArrivalTime { get; set; }
      [Column("EventCapacity")]
      public int SessionCapacity { get; set; }
  
      //navigation properties        
      [JsonIgnore]        
      public virtual int EventId { get; set; }
      [JsonIgnore]        
      public virtual Event Event { get; set; }            
  }
  </code></pre>
  
  <p>My DbContext</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder) 
  {       
      modelBuilder.Entity&lt;Event&gt;()
          .HasOne(e =&gt; e.Brand)
          .WithMany(b =&gt; b.Events).HasForeignKey(e=&gt;e.BrandId);
  
      modelBuilder.Entity&lt;Event&gt;()
          .HasMany(s =&gt; s.Sessions)
          .WithOne(e =&gt; e.Event).HasForeignKey(s =&gt; s.EventId);
  
      modelBuilder.Entity&lt;Event&gt;(entity=&gt; {
          entity.Property(e =&gt; e.EventId).HasColumnName("EventID");
          entity.HasKey(e =&gt; new{ e.EventId, e.EventCloseDate});
          entity.HasIndex(e =&gt; e.EventId).HasName("For Full Text Indexing").IsUnique();
          entity.Property(e =&gt; e.Active).HasDefaultValue(false);
          entity.Property(e =&gt; e.EventCloseDate)
              .HasColumnType("datetime")
              .HasDefaultValueSql("'1/1/2038'");
          entity.Property(e =&gt; e.Name).HasMaxLength(1024);
          entity.Property(e =&gt; e.PaxAllocationLimit).HasDefaultValue(10000);
      });
  
      modelBuilder.Entity&lt;Brand&gt;(entity =&gt; {
          entity.HasKey(e =&gt; e.BrandId);                
  
          entity.Property(e =&gt; e.Active).HasDefaultValue(false);               
  
          entity.Property(e =&gt; e.Name)
              .IsRequired()
              .HasMaxLength(150)
              .HasColumnType("varchar");                
      });
  
      modelBuilder.Entity&lt;Session&gt;(entity =&gt; {
          entity.HasKey(e =&gt; e.SessionId);
  
          entity.Property(e=&gt;e.Name)
            .HasMaxLength(250)
              .HasColumnType("varchar")
              .HasDefaultValue("");
  
          entity.Property(e =&gt; e.SessionDurationInMinutes)
              .HasColumnType("numeric")
              .HasDefaultValue(0m);                
          });
      }
  
      public virtual DbSet&lt;Brand&gt; Brands { get; set; }
      public virtual DbSet&lt;Event&gt; Events { get; set; }        
      public virtual DbSet&lt;Session&gt; Sessions { get; set; }
  }
  </code></pre>
  
  <p>I'm using the project as a webapi, when I call up Brands, it generates the following SQL:</p>
  
  <pre><code>SELECT [e].[brandingId], [e].[BrandingActive], [e].[DeadBrand], [e].[BrandingSiteTitle]
  FROM [tblBranding] AS [e]
  WHERE [e].[BrandingActive] = 1
  ORDER BY [e].[BrandingSiteTitle], [e].[brandingId]
  Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (75ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  SELECT [t].[EventId], [t].[EventCloseDate], [t].[eventActive], [t].[brandingId], [t].[EventId1], [t].[eventName], [t].[PaxAllocationLimit]
  FROM [tblEvents] AS [t]
  INNER JOIN (
      SELECT DISTINCT [e].[BrandingSiteTitle], [e].[brandingId]
      FROM [tblBranding] AS [e]
      WHERE [e].[BrandingActive] = 1
  ) AS [e] ON [t].[brandingId] = [e].[brandingId]
  ORDER BY [e].[BrandingSiteTitle], [e].[brandingId]
  </code></pre>
  
  <p>where [t].[EventId1] column name is invalid
  note that If I comment out the following code in DbContext - this error goes away and the query is generated correctly:</p>
  
  
  
  <pre><code>modelBuilder.Entity&lt;Event&gt;()
              .HasMany(s =&gt; s.Sessions)
              .WithOne(e =&gt; e.Event).HasForeignKey(s =&gt; s.EventId);
  </code></pre>
  
  <p>I have tried adding [ForeignKey], [InverseProperty] attributes while 'fiddling' - it doesn't seem to make a difference</p>
  
  <p>I also tried adding the column name explicitly as mentioned <a href="https://stackoverflow.com/questions/36413461/entity-framework-7-invalid-column-name">here</a></p>
  
  <p>I'm not sure what else to try - it starts happening only when I define the relationship to Sessions in FluidUI - suggestions ?</p>