title: Entity Framework Core 2.1 trouble with relationships
body: <p>I'm trying to convert the following sql query to entity framework but running into problems with what appears columns not joining to tables.</p>
  
  <pre><code>SELECT 
  a.TABLE_NAME AS tableName,
  b.COLUMN_NAME AS columnName,
  b.DATA_TYPE AS dataType,
  CASE WHEN b.IS_NULLABLE = 'NO' THEN 'FALSE' ELSE 'TRUE' END AS allowNull
  FROM INFORMATION_SCHEMA.TABLES a
  INNER JOIN INFORMATION_SCHEMA.COLUMNS b ON a.TABLE_NAME = b.TABLE_NAME
  </code></pre>
  
  <p>This is what i have so far</p>
  
  <p>DB Context:</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  
  namespace EFCoreTest.Models 
  {
      public class InformationContext : DbContext
      {   
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
          {
              optionsBuilder.UseSqlServer(@"Server=localhost;Database=master;Trusted_Connection=True;");
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Table&gt;()
                  .HasKey(t =&gt; new {t.tableName, t.catalogName, t.schemaName});
  
              modelBuilder.Entity&lt;Column&gt;()
                  .HasOne(c =&gt; c.table)
                  .WithMany(c =&gt; c.columns)
                  .HasForeignKey(c =&gt; new {c.tableName, c.catalogName, c.schemaName});
  
          }
  
          public DbSet&lt;Table&gt; Tables {get; set;}
          public DbSet&lt;Column&gt; Columns {get; set;}
      }
  }
  </code></pre>
  
  <p>Column Class:</p>
  
  <pre><code>using System;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace EFCoreTest.Models
  {
      [Table("COLUMNS", Schema = "INFORMATION_SCHEMA")]
      public class Column
      {
          [Key]
          [Column("COLUMN_NAME")]
          public String columnName {get; set;}
          [Column("DATA_TYPE")]
          public String dataType {get; set;}
          [Column("IS_NULLABLE")]
          public String allowNUlls {get; set;}
          [ForeignKey("Table")]
          [Column("TABLE_NAME")]
          public String tableName {get; set;}
          [ForeignKey("Table")]
          [Column("TABLE_CATALOG")]
          public String catalogName {get; set;}
          [ForeignKey("Table")]
          [Column("TABLE_SCHEMA")]
          public String schemaName {get; set;}
          public Table table {get; set;}
  
      }
  }
  </code></pre>
  
  <p>Table class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace EFCoreTest.Models
  {
      [Table("TABLES" , Schema = "INFORMATION_SCHEMA")]
      public class Table
      {
          [Key]
          [Column("TABLE_NAME")]
          public String tableName {get; set;}
          [Key]
          [Column("TABLE_CATALOG")]
          public String catalogName {get; set;}
          [Key]
          [Column("TABLE_SCHEMA")]
          public String schemaName {get; set;}
          public ICollection&lt;Column&gt; columns {get; set;}
  
          protected Table() {columns = new List&lt;Column&gt;();}
      }
  }
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>using System;
  using Microsoft.EntityFrameworkCore;
  using System.Linq;
  using EFCoreTest.Models;
  
  namespace EFCoreTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              using(InformationContext context = new InformationContext())
              {
                  var results = context.Tables.Include(t =&gt; t.columns).ToList();
  
                  foreach(var t in results)
                  {
                      Console.WriteLine(t.tableName);
                      Console.WriteLine("-----------------------------");
                      var columns = t.columns.ToList();
  
                      foreach(var c in columns)
                      {
                          Console.WriteLine(c.columnName);
                      }
  
                      Console.WriteLine("");
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Code runs fine, but when inspecting the table instances all the column instances are null. I have a feeling it has something to do with the relationship between table and column but after going over the docs for relationships for efcore2.1 i cant work out what I'm doing wrong. </p>
  
  <p>Any help would be appreciated.</p>
  
  <p><strong>Update:</strong>
  Updated code with additional keys and related data loading.</p>