tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
1.79k stars 586 forks source link

Problems with class mapper #323

Open leonardopottmayer opened 1 year ago

leonardopottmayer commented 1 year ago

Hello. I'm having some trouble while trying to use ClassMapper...

My entity class:

public class Product : AuditableEntity
    {
        public Product() { }
        public Product(int id, string name, decimal price)
        {
            Id = id;
            Name = name;
            Price = price;
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

My mapper class:

public class ProductMapper : AuditableEntityClassMapper<Product>
    {
        public ProductMapper()
        {
            Table("product");

            Map(i => i.Id).Column("id").Key(KeyType.Identity);
            Map(i => i.Name).Column("name_desc");
            Map(i => i.Price).Column("price");
        }
    }

Additional class:

public abstract class AuditableEntityClassMapper<TEntity> : ClassMapper<TEntity> where TEntity : AuditableEntity
    {
        public AuditableEntityClassMapper()
        {
            Map(i => i.CreatedBy).Column("created_by");
            Map(i => i.CreatedAt).Column("created_at");
            Map(i => i.UpdatedBy).Column("updated_by");
            Map(i => i.UpdatedAt).Column("updated_at");
        }
    }

The stacktrace it generates:

MySqlConnector.MySqlException (0x80004005): Unknown column 'y_1.Name' in 'field list'
   at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /_/src/MySqlConnector/Core/ResultSet.cs:line 43
   at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 130

It seems like it's ignoring the .Column() method in class mapper and it's generating the SQL sentence based on the class attributes. There's an example in stacktrace, with "Name" and "name_desc". But the same error happens with "CreatedAt" and "created_at", "UpdatedBy" and "updated_by", etc.