3

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.

But when I am trying to use it for more complex operations like join, it raises an exception.

The error message says: SqlException: Need declare the scalar variable "@product_id".

But in single Get< T > it works.

Packages: Dapper Dommel Dapper-FluentMap.Dommel Dapper-FluentMap

GitHub Dommel https://github.com/henkmollema/Dommel

Does someone managed to use Dapper Dommel to return multiple entities in join queries with automatic mapping ?

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() => Connection.Close();
}

Dommel fluent map:

 public class ProductMap : DommelEntityMap<Product>
    {
        public ProductMap()
        {
            ToTable("production.products");
            Map(m => m.Id)
                .ToColumn("product_id")
                .IsKey()
                .IsIdentity();

            Map(m => m.Name)
                .ToColumn("product_name");

            Map(m => m.BrandId)
                .ToColumn("brand_id");

            Map(p => p.CategoryId)
                .ToColumn("category_id");

            Map(p => p.ModelYear)
                .ToColumn("model_year");

            Map(p => p.ListPrice)
                .ToColumn("list_price");
        }
    }

Category mapping:

public class CategoryMap : DommelEntityMap<Category>
{
    public CategoryMap()
    {
        ToTable("production.categories");
        Map(m => m.Id)
            .ToColumn("category_id")
            .IsKey()
            .IsIdentity();

        Map(m => m.Name)
            .ToColumn("category_name");
    }
}

Register mappings:

public RegisterMappings()
{
    FluentMapper.Initialize(config =>
    {
        config.AddMap(new ProductMap());
        config.AddMap(new CategoryMap());
        config.ForDommel();
    });
}

Model:

   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; }
    }

Get simple that works:

        using (var connect = new DbContext())
        {
            var product = connect.Connection.Get<Product>(id);
            return product;
        }

Get Doesn't work:

    using (var connect = new DbContext())
    {
        var prod = connect.Connection.Get<Product, Category, Product>(1, (product, category) =>
        {
            product.Category = category;
            return product;
        });

        return prod;
    }

0 Answers0