title: How to create a table relation in Orchard?
body: <p>I want the MachineRecord to contain a GroupRecord Id, like the following:</p>
  
  <p><strong>MachineRecord</strong></p>
  
  <p>Id<br>
  Name<br>
  Number<br>
  GroupRecord  </p>
  
  <p><strong>GroupRecord</strong></p>
  
  <p>Id<br>
  Name</p>
  
  <p>What I have tried:</p>
  
  <p>I have tried to make the property in the model a<br>
  <code>public virtual GroupRecord GroupRecord { get; set; }</code><br>
  and the database column name <code>GroupRecord_Id</code>, but somehow that won't work, tried some other ways like giving the property the name <code>GroupRecordId</code> and the table column <code>GroupRecord</code> and so on, but all with no results.</p>
  
  <p>So the question is, how to get this to work so the MachineRecord table contains a GroupRecord Id?</p>
  
  <p><em>Machine.cs</em></p>
  
  <pre><code>namespace PowerAll.Voorraad.Models
  {
    public class MachineRecord
    {
      public virtual int Id { get; set; }
      public virtual int MachineNumber { get; set; }
      public virtual string Title { get; set; }
      public virtual string Description1 { get; set; }
      public virtual string Description2 { get; set; }
      public virtual string Description3 { get; set; }
      public virtual string Description4 { get; set; }
      public virtual string Description5 { get; set; }
      public virtual string Description6 { get; set; }
      public virtual string SerialNumber { get; set; }
      public virtual GroupRecord GroupRecord { get; set; }
      public virtual char PriceType { get; set; }
      public virtual decimal Price { get; set; }
      public virtual int Year { get; set; }
    }
  }
  </code></pre>
  
  <p><em>Group.cs</em></p>
  
  <pre><code>namespace PowerAll.Voorraad.Models
  {
    public class GroupRecord
    {
      public virtual int Id { get; set; }
      public virtual string GroupName { get; set; }
    }
  }
  </code></pre>
  
  <p><em>Migrations.cs</em></p>
  
  <pre><code>namespace PowerAll.Voorraad
  {
    public class Migrations : DataMigrationImpl
    {
      public int Create()
      {
        SchemaBuilder.CreateTable("GroupRecord", table =&gt; table
          .Column&lt;int&gt;("Id", column =&gt; column.PrimaryKey().Identity())
          .Column&lt;string&gt;("GroupName", column =&gt; column.WithLength(100))
        );
  
        SchemaBuilder.CreateTable("MachineRecord", table =&gt; table
          .Column&lt;int&gt;("Id", column =&gt; column.PrimaryKey().Identity())
          .Column&lt;int&gt;("MachineNumber", column =&gt; column.NotNull())
          .Column&lt;string&gt;("Title", column =&gt; column.NotNull().WithLength(40))
          .Column&lt;string&gt;("Description1", column =&gt; column.WithLength(70))
          .Column&lt;string&gt;("Description2", column =&gt; column.WithLength(70))
          .Column&lt;string&gt;("Description3", column =&gt; column.WithLength(70))
          .Column&lt;string&gt;("Description4", column =&gt; column.WithLength(70))
          .Column&lt;string&gt;("Description5", column =&gt; column.WithLength(70))
          .Column&lt;string&gt;("Description6", column =&gt; column.WithLength(70))
          .Column&lt;string&gt;("SerialNumber", column =&gt; column.WithLength(20))
          .Column&lt;int&gt;("GroupRecord_id", column =&gt; column.NotNull())
          .Column&lt;char&gt;("PriceType", column =&gt; column.NotNull().WithLength(1))
          .Column&lt;decimal&gt;("Price", column =&gt; column.NotNull())
          .Column&lt;int&gt;("Year", column =&gt; column.WithLength(4))
        );
  
        // Return the version that this feature will be after this method completes
        return 1;
      }
    }
  }
  </code></pre>
  
  <p><em>MachineController.cs</em></p>
  
  <pre><code>namespace PowerAll.Voorraad.Controllers
  {
    [Themed]
    public class MachineController : Controller
    {
      private readonly IRepository&lt;MachineRecord&gt; machineRecords;
  
      public MachineController(IRepository&lt;MachineRecord&gt; MachineRecords) {
          machineRecords = MachineRecords;
      }
  
      public ActionResult List()
      {
          var items = machineRecords.Table;
  
          return View(items);
      }
    }
  }
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>I came this far thanks to endorphin, but when I try to execute <code>AddDummyData()</code> I get this error: <code>could not insert: [PowerAll.Voorraad.Models.MachineRecord][SQL: INSERT INTO PowerAll_Voorraad_MachineRecord (MachineNumber, Title, Description1, Description2, Description3, Description4, Description5, Description6, SerialNumber, PriceType, Price, Year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]</code> While when I step trough the code all values are being posted right.</p>
  
  <p>This is my AddDummyData():</p>
  
  <pre><code>public void AddDummyData()
  {
    var GroupRecord = new GroupRecord { GroupName = "Oldtimers" };
  
    groupRecords.Create(GroupRecord);
  
    var MachineRecord = new MachineRecord
    {
      MachineNumber = 100001,
      Title = "Landini L25",
      Description1 = "Desc 1",
      Description2 = "Desc 2",
      Description3 = "Desc 3",
      Description4 = "Desc 4",
      Description5 = "Desc 5",
      Description6 = "Desc 6",
      SerialNumber = "100000",
      GroupRecord = GroupRecord,
      PriceType = 'I',
      Price = 7999.99m,
      Year = 1954
    };
  
    machineRecords.Create(MachineRecord);
  }
  </code></pre>
  
  <p>EDIT 3:</p>
  
  <p>Now it's working, this is my working code (for those who are struggling on this too)</p>
  
  <p><em>Migrations.cs</em></p>
  
  <pre><code>  public class Migrations : DataMigrationImpl
    {
      public int Create()
      {
        SchemaBuilder.CreateTable("MachineRecord", table =&gt; table
          .Column&lt;int&gt;("Id", column =&gt; column.PrimaryKey().Identity())
          .Column&lt;int&gt;("GroupRecord_Id")
          .Column&lt;int&gt;("MachineNumber", column =&gt; column.NotNull())
          .Column&lt;string&gt;("Title", column =&gt; column.NotNull().WithLength(40))
          .Column&lt;string&gt;("Description", column =&gt; column.WithLength(70))
          .Column&lt;char&gt;("PriceType", column =&gt; column.NotNull().WithLength(1))
          .Column&lt;decimal&gt;("Price", column =&gt; column.NotNull())
          .Column&lt;int&gt;("Year", column =&gt; column.WithLength(4))
        );
  
        SchemaBuilder.CreateTable("GroupRecord", table =&gt; table
          .Column&lt;int&gt;("Id", column =&gt; column.PrimaryKey().Identity())
          .Column&lt;string&gt;("Name")
        );
  
        return 1;
      }
    }
  </code></pre>
  
  <p><em>MachineRecord.cs</em></p>
  
  <pre><code>  public class MachineRecord
    {
      public virtual int Id { get; set; }
      public virtual int MachineNumber { get; set; }
      public virtual string Title { get; set; }
      public virtual string Description { get; set; }
      public virtual GroupRecord GroupRecord { get; set; }
      public virtual char PriceType { get; set; }
      public virtual decimal Price { get; set; }
      public virtual int Year { get; set; }
    }
  </code></pre>
  
  <p><em>GroupRecord.cs</em></p>
  
  <pre><code>  public class GroupRecord
    {
      public virtual int Id { get; set; }
      public virtual string Name { get; set; }
    }
  </code></pre>