title: Dapper Dommel - query join automatic mapping
body: <p>I'm sorry for my bad english, but here it goes, I am using Dapper with Dapper Dommel to simplify  operations like crud, but in Dommel's Github says that it supports Join operations too, so I'm trying to implement it on my code, so far I managed to return a simple entity with Get. </p>
  
  <p>But when I am trying to use it for more complex operations like join, it raises an exception.</p>
  
  <p>The error message says: SqlException: <em>Need declare the scalar variable "@product_id".</em></p>
  
  <p>But in single <em>Get&lt; T ></em> it works.</p>
  
  <p>Packages: 
  Dapper
  Dommel
  Dapper-FluentMap.Dommel
  Dapper-FluentMap</p>
  
  <p>GitHub Dommel
  <a href="https://github.com/henkmollema/Dommel" rel="nofollow noreferrer">https://github.com/henkmollema/Dommel</a></p>
  
  <p><strong>Does someone managed to use Dapper Dommel to return multiple entities in join queries with automatic mapping ?</strong></p>
  
  <pre><code>public class DbContext : IDisposable
  {
      public SqlConnection Connection { get; private set; }
  
      public DbContext()
      {
          Connection = new SqlConnection("Server=localhost;Database=BikeStores;Trusted_Connection=True;");
          OpenConnection();
      }
  
      private bool OpenConnection()
      {
          try
          {
              if (Connection.State != System.Data.ConnectionState.Open)
                  Connection.Open();
              return true;
          }
          catch (Exception ex)
          {
              Debug.WriteLine(ex.Message);
              return false;
          }
      }
  
      public void Dispose() =&gt; Connection.Close();
  }
  </code></pre>
  
  <p>Dommel fluent map:</p>
  
  <pre><code> public class ProductMap : DommelEntityMap&lt;Product&gt;
      {
          public ProductMap()
          {
              ToTable("production.products");
              Map(m =&gt; m.Id)
                  .ToColumn("product_id")
                  .IsKey()
                  .IsIdentity();
  
              Map(m =&gt; m.Name)
                  .ToColumn("product_name");
  
              Map(m =&gt; m.BrandId)
                  .ToColumn("brand_id");
  
              Map(p =&gt; p.CategoryId)
                  .ToColumn("category_id");
  
              Map(p =&gt; p.ModelYear)
                  .ToColumn("model_year");
  
              Map(p =&gt; p.ListPrice)
                  .ToColumn("list_price");
          }
      }
  </code></pre>
  
  <p>Category mapping:</p>
  
  <pre><code>public class CategoryMap : DommelEntityMap&lt;Category&gt;
  {
      public CategoryMap()
      {
          ToTable("production.categories");
          Map(m =&gt; m.Id)
              .ToColumn("category_id")
              .IsKey()
              .IsIdentity();
  
          Map(m =&gt; m.Name)
              .ToColumn("category_name");
      }
  }
  </code></pre>
  
  <p>Register mappings:</p>
  
  <pre><code>public RegisterMappings()
  {
      FluentMapper.Initialize(config =&gt;
      {
          config.AddMap(new ProductMap());
          config.AddMap(new CategoryMap());
          config.ForDommel();
      });
  }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>   public class Product
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public int BrandId { get; set; }
          public int CategoryId { get; set; }
          public Category Category { get; set; }
          public int ModelYear { get; set; }
          public decimal ListPrice { get; set; }
      }
  </code></pre>
  
  <p>Get simple that works: </p>
  
  <pre><code>        using (var connect = new DbContext())
          {
              var product = connect.Connection.Get&lt;Product&gt;(id);
              return product;
          }
  </code></pre>
  
  <p>Get Doesn't work:</p>
  
  <pre><code>    using (var connect = new DbContext())
      {
          var prod = connect.Connection.Get&lt;Product, Category, Product&gt;(1, (product, category) =&gt;
          {
              product.Category = category;
              return product;
          });
  
          return prod;
      }
  </code></pre>