title: Simple.Data > Object inside another object not populated
body: <p>I have 2 tables as below:</p>
  
  <p>Table: Orders </p>
  
  <pre><code>CREATE TABLE [dbo].[orders](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [ticker] [char](5) NOT NULL,
      [broker_id] [char](12) NOT NULL,
      [type] [smallint] NOT NULL,
      [quantity] [int] NOT NULL,
      [price] [money] NOT NULL,
   CONSTRAINT [PK_orders] PRIMARY KEY CLUSTERED 
  (
      [id] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  
  GO
  
  SET ANSI_PADDING OFF
  GO
  
  ALTER TABLE [dbo].[orders]  WITH CHECK ADD  CONSTRAINT [FK_orders_quotes] FOREIGN KEY([ticker])
  REFERENCES [dbo].[quotes] ([ticker])
  ON UPDATE CASCADE
  ON DELETE CASCADE
  GO
  
  ALTER TABLE [dbo].[orders] CHECK CONSTRAINT [FK_orders_quotes]
  GO
  
  ALTER TABLE [dbo].[orders]  WITH CHECK ADD  CONSTRAINT [FK_orders_types] FOREIGN KEY([type])
  REFERENCES [dbo].[order_types] ([id])
  ON UPDATE CASCADE
  ON DELETE CASCADE
  GO
  
  ALTER TABLE [dbo].[orders] CHECK CONSTRAINT [FK_orders_types]
  GO
  
  ALTER TABLE [dbo].[orders]  WITH CHECK ADD  CONSTRAINT [FK_orders_users] FOREIGN KEY([broker_id])
  REFERENCES [dbo].[users] ([id])
  ON UPDATE CASCADE
  ON DELETE CASCADE
  GO
  
  ALTER TABLE [dbo].[orders] CHECK CONSTRAINT [FK_orders_users]
  GO
  </code></pre>
  
  <p>Table: users</p>
  
  <pre><code>CREATE TABLE [dbo].[users](
      [id] [char](12) NOT NULL,
      [password] [varchar](60) NULL,
      [first_name] [varchar](20) NULL,
      [last_name] [varchar](20) NULL,
      [email] [varchar](20) NULL,
      [date_of_birth] [datetime] NULL,
      [capital] [money] NULL,
      [created_by] [varchar](12) NULL,
      [created_date] [datetime] NULL,
   CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED 
  (
      [id] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  
  GO
  
  SET ANSI_PADDING OFF
  GO
  
  ALTER TABLE [dbo].[users] ADD  DEFAULT ((0.0)) FOR [capital]
  GO
  </code></pre>
  
  <p>And I have these 2 classes that map to the tables above:</p>
  
  <p>Order</p>
  
  <pre><code>public class Order
  {
      public Order(string ticker, User user, OrderTypes type, int quantity, decimal price)
      {
          this.Ticker = ticker;
          this.Broker = user;
          this.Type = type;
          this.Quantity = quantity;
          this.Price = price;
      }
      public Order() { }
  
      public string Ticker { get; private set; }
      public OrderTypes Type { get; private set; }
      public int Quantity { get; private set; }
      public decimal Price { get; private set; }
  
      public User Broker { get; private set; }
  }
  </code></pre>
  
  <p>User</p>
  
  <pre><code>public class User
      {
          public User() { }
  
          public User(string id, string firstName, string lastName, DateTime dateOfBirth, string email, decimal capital)
          {
              this.Id = id;
              this.FirstName = firstName;
              this.LastName = lastName;
              this.DateOfBirth = dateOfBirth;
              this.Email = email;
              this.Capital = capital;
          }
  
          public User(string id, string firstName, string lastName, DateTime dateOfBirth, string email) : this(id, firstName, lastName, dateOfBirth, email, 0) { }
  
          public string Id { get; private set; }
          public string FirstName { get; private set; }
          public string LastName { get; private set; }
          public DateTime DateOfBirth { get; private set; }
          public string Email { get; private set; }
          public decimal Capital { get; private set; }
      }
  </code></pre>
  
  <p>Finally, I have these two lines of code:</p>
  
  <pre><code>db.Orders.Insert(id: 1, broker_id: "john.doe", quantity: 100);
  Order order = db.Orders.Get(1);
  </code></pre>
  
  <p>The object order has the properties "Id" and "Quantity" populated, but the object "Broker" is null.</p>
  
  <p>So I tried to retrieve the data like shown below, but still no luck: the Broker object is always NULL:</p>
  
  <pre><code>var order = db.Orders.FindAllById(1)
                  .Join(db.Users.As("Broker"), out user)
                  .On(Id: db.Orders.Broker_Id)
                  .With(user.As("Broker"))
                  .FirstOrDefault();
  
  WITH __Data AS (SELECT [dbo].[orders].[id], ROW_NUMBER() OVER(ORDER BY [dbo].[orders].[id]) AS [_#_]
  from [dbo].[orders] JOIN [dbo].[users] [Broker] ON ([Broker].[id] = [dbo].[orders].[broker_id]) WHERE [dbo].[orders].[id] = @p1)
  SELECT [dbo].[orders].[id],[dbo].[orders].[ticker],[dbo].[orders].[broker_id],[dbo].[orders].[type],[dbo].[orders].[quantity],[dbo].[orders].[price],[Broker].[id] AS [__withn__Broker__id],[Broker].[password] AS [__withn__Broker__password],[Broker].[first_name] AS [__withn__Broker__first_name],[Broker].[last_name] AS [__withn__Broker__last_name],[Broker].[email] AS [__withn__Broker__email],[Broker].[date_of_birth] AS [__withn__Broker__date_of_birth],[Broker].[capital] AS [__withn__Broker__capital],[Broker].[created_by] AS [__withn__Broker__created_by],[Broker].[created_date] AS [__withn__Broker__created_date] FROM __Data JOIN [dbo].[orders] ON [dbo].[orders].[id] = __Data.[id] JOIN [dbo].[users] [Broker] ON ([Broker].[id] = [dbo].[orders].[broker_id]) WHERE [dbo].[orders].[id] = @p1 AND [_#_] BETWEEN 1 AND 1
  Parameters:
    @p1 (Int32) = 1
  </code></pre>
  
  <hr>
  
  <pre><code>order = db.Orders.FindAllById(1)
              .Join(db.Users.As("Broker"), out user)
              .On(db.Orders.broker_id == user.Id)
              .With(user)
              .FirstOrDefault();
  
  WITH __Data AS (SELECT [dbo].[orders].[id], ROW_NUMBER() OVER(ORDER BY [dbo].[orders].[id]) AS [_#_]
  from [dbo].[orders] JOIN [dbo].[users] [Broker] ON ([dbo].[orders].[broker_id] = [Broker].[id]) WHERE [dbo].[orders].[id] = @p1)
  SELECT [dbo].[orders].[id],[dbo].[orders].[ticker],[dbo].[orders].[broker_id],[dbo].[orders].[type],[dbo].[orders].[quantity],[dbo].[orders].[price],[Broker].[id] AS [__withn__Broker__id],[Broker].[password] AS [__withn__Broker__password],[Broker].[first_name] AS [__withn__Broker__first_name],[Broker].[last_name] AS [__withn__Broker__last_name],[Broker].[email] AS [__withn__Broker__email],[Broker].[date_of_birth] AS [__withn__Broker__date_of_birth],[Broker].[capital] AS [__withn__Broker__capital],[Broker].[created_by] AS [__withn__Broker__created_by],[Broker].[created_date] AS [__withn__Broker__created_date] FROM __Data JOIN [dbo].[orders] ON [dbo].[orders].[id] = __Data.[id] JOIN [dbo].[users] [Broker] ON ([dbo].[orders].[broker_id] = [Broker].[id]) WHERE [dbo].[orders].[id] = @p1 AND [_#_] BETWEEN 1 AND 1
  Parameters:
    @p1 (Int32) = 1
  </code></pre>
  
  <hr>
  
  <pre><code>order = db.Orders.With(db.Users.As("Broker")).Get(1); 
  
  WITH __Data AS (SELECT [dbo].[orders].[id], ROW_NUMBER() OVER(ORDER BY [dbo].[orders].[id]) AS [_#_]
  from [dbo].[orders] WHERE [dbo].[orders].[id] = @p1)
  SELECT [dbo].[orders].[id],[dbo].[orders].[ticker],[dbo].[orders].[broker_id],[dbo].[orders].[type],[dbo].[orders].[quantity],[dbo].[orders].[price] FROM __Data JOIN [dbo].[orders] ON [dbo].[orders].[id] = __Data.[id] WHERE [dbo].[orders].[id] = @p1 AND [_#_] BETWEEN 1 AND 1
  Parameters:
    @p1 (Int32) = 1
  </code></pre>
  
  <blockquote>
    <blockquote>
      <p>Note: when assigning the order to a type of variable "var", the order does have the user information populated.
      So, if I do this:</p>
    </blockquote>
  </blockquote>
  
  <p>var dynamicOrder = db.Orders.WithUser().Get(1);</p>
  
  <p>the "dynamicOrder" is of type "Simple.Data.SimpleRecord" which has inside the following property "System.Collections.Generic.IDictionary.Values" that contains the "user" data inside.</p>
  
  <p>So the issue appears to be with the casting of the user data to the user class.</p>
  
  <p>Thoughts?</p>