henkmollema / Dommel

CRUD operations with Dapper made simple.
MIT License
634 stars 100 forks source link

Invalid Column Map #48

Closed IvAlex1986 closed 8 years ago

IvAlex1986 commented 8 years ago

Hello, We have the trouble with Dommel.

We use: MySQL 5.6.15 + Dapper 1.50.2 + Dommel 1.6.0 + Dapper.FluentMap.Dommel 1.4.0

Our Model:

public class AppCityList
{
    public int Id { get; set; }
    public string CityName { get; set; }
    public int StateId { get; set; }
    public int CountryId { get; set; }
}

Our Mapping class:

public class AppCityListMap : DommelEntityMap<AppCityList>
{
    public AppCityListMap()
    {
        ToTable("app_city_list");
        Map(p => p.Id).IsKey();
        Map(p => p.CityName).ToColumn("City_Name");
        Map(p => p.StateId).ToColumn("State_Id");
        Map(p => p.CountryId).ToColumn("Country_Id");
    }
}

We have added mapper using next command:

FluentMapper.Initialize(config => { config.AddMap(new AppCityListMap()); });

We try to run query using next command:

private TU GetSingle<TU>(Expression<Func<TU, bool>> predicate)
{
    using (var con = ConnectionFactory.GetReadOnlyConnection())
    {
        var connection = con;
        return Action(con, () =>
        {
            var entities = connection.Select(predicate);
            return entities.FirstOrDefault();
        });
    }
}

public AppCityList GetCityByNameAndStateIdAndCountryId(String cityName, Int32 stateId, Int32 countryId)
{
    return GetSingle<AppCityList>(a => a.CityName == cityName && a.CountryId == countryId && a.StateId == stateId);
}

But our SQL looks like this: select * from app_city_list where CityName = @p0 and CountryId = @p1 and StateId = @p2

And we have an error: Unknown column 'CityName' in 'where clause'

This error occurs because only City_Name column exists in our table app_city_list. Could you help with the problem of improper mapping work?

henkmollema commented 8 years ago

You're probably missing the Dommel integration in your Dapper.FluentMap configuration. You have to call ForDommel() on the configuration instance like this:

FluentMapper.Initialize(config => 
    { 
        config
            .AddMap(new AppCityListMap())
            .ForDommel(); 
    });

This registers the table and property name resolvers for Dommel using the mapping defined with Dapper.FluentMap. This behavior is implemented by the Dapper.FluentMap.Dommel package.

Please let me know if this resolves your issue.

huhubun commented 8 years ago

Hi Henk, I have this problem too, and I'm very sure I called config.ForDommel()

    FluentMapper.Initialize(config =>
    {
        config.AddMap(new HotelMap());
        config.ForDommel();
    });

But there are a little different in entity class, it derived from a base class

    public class Hotel : BaseEntity
    {
        public string Description { get; set; }
    }

    public class BaseEntity
    {
        public string Code { get; set; }
    }

mapping class is

    public class HotelMap : DommelEntityMap<Hotel>
    {
        public HotelMap()
        {
            ToTable("hotels");

            Map(t => t.Description).ToColumn("des");
            Map(t => t.Code).ToColumn("hotel_code");
        }
    }

If the Code property defined in the Hotel class, and everything is OK. I use Dapper 1.50.2 + Dommel 1.6.1 + Dapper.FluentMap.Dommel 1.4.2, and also tried Dommel 1.7.1

Could you make it work with base class? Thanks.

jmreynolds commented 8 years ago

@huhubun - I think that your problem might be fixed in Dapper.FluentMap 1.5.1 - see The corresponding Pull Request

huhubun commented 8 years ago

@jmreynolds - I tried the version you mentioned, but it seems the issue hasn't been fixed, thanks anyway.

henkmollema commented 8 years ago

@huhubun there is a indeed an issue with derived entities. I still have to look into that...

henkmollema commented 8 years ago

@IvAlex1986 is your issue resolved? Please re-open if it's not.