title: EF Core: loading related entities - circular dependency
body: <p>I have 2 related entities in EF Core (database first design from an existing database) and having trouble loading one-many relationship - it's a webapi ASP.NET core 1.0 application</p>
  
  <p>Brand Entity</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; }
  }
  </code></pre>
  
  <p>Event entity:</p>
  
  <pre><code>[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                
      [Column("BrandingId")]
      public int BrandId { get; set; }        
      public virtual Brand Brand { get; set; }
      public virtual ICollection&lt;Session&gt; Sessions { get; set; }
  
  }
  </code></pre>
  
  <p>code from for FLUID API in OnModelCreating in DbContext:</p>
  
  <pre><code>modelBuilder.Entity&lt;Event&gt;()
              .HasOne(e =&gt; e.Brand)
              .WithMany(b =&gt; b.Events).HasForeignKey(e=&gt;e.BrandId);
  
      public virtual DbSet&lt;Brand&gt; Brands { get; set; }
      public virtual DbSet&lt;Event&gt; Events { get; set; }  
  </code></pre>
  
  <p>code from BrandsController:</p>
  
  <pre><code>    [HttpGet]
      public IActionResult Get() {
          //var brands = from b in _context.Brands
          //             where b.Active == true
          //             orderby b.BrandName
          //             select b;
  
          var brands = _context.Brands.Include(e =&gt; e.Events).Where(b =&gt; b.Active == true).OrderBy(b =&gt; b.Name);
  
          return new ObjectResult(brands);
      }   
  </code></pre>
  
  <p>code from EventsController</p>
  
  <pre><code> // GET: api/values
      [HttpGet("{id:int?}")]
      public IActionResult Get(int? id) {
          var events = from e in _context.Events
                       where e.Active == true
                       orderby e.Name
                       select e;
  
          if (!events.Any()) {
              return HttpNotFound();
          }
  
          if (id != null) {
              events = events.Where(e =&gt; e.EventId == id).OrderBy(e =&gt; 0);
              if (events.Count() == 0) { return HttpNotFound(); }
              return new ObjectResult(events);
          }
          else {
              return new ObjectResult(events);
          }
      }
  </code></pre>
  
  <p>When I try to load brands through the API, I get an exception:</p>
  
  <p>Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (80ms) [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]
  Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory: Error: An exception occurred in the database while iterating the results of a query.
  System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'EventId1'.</p>
  
  <p>Apart from this, is there a way to load related entities without using the "Includes" ? If I do not use include and use the standard LINQ query, the related entities are loaded as NULL</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm now getting an invalid column error - i noticed in my previous code I hadn't used virtual on the ICollection in brand</p>
  
  <p>now i can't figure out why is it generating EventId1 column in the SQL</p>
  
  <p>EF 7 version is 1.0.0-rc1-final</p>
  
  <p><strong>UPDATE-2</strong>
  After playing around with the code the Exception changed to circular dependency exception in code given the exact same code as above - I don't know why it was generating the invalid column name earlier (EventId1)</p>