henkmollema / Dapper-FluentMap

Provides a simple API to fluently map POCO properties to database columns when using Dapper.
MIT License
429 stars 88 forks source link

Can't seem to get it to work... #38

Closed richarddavenport closed 7 years ago

richarddavenport commented 8 years ago

Here's my model:

public class HouseAccount
{
    public string ServiceCenter { get; set; }
    public string ServiceUnit { get; set; }
    public string AccountNumber { get; set; }
    public string AccountName { get; set; }
}

Here's my map:

public class HouseAccountMap : EntityMap<HouseAccount>
    {
        public void ProductMap()
        {
            Map(a => a.ServiceCenter)
                .ToColumn("parent_region_id");

            Map(a => a.ServiceUnit)
                .ToColumn("region_id");

            Map(a => a.AccountNumber)
                .ToColumn("cust_id");

            Map(a => a.AccountName)
                .ToColumn("bo_name");
        }
    }

Here's my sql

SELECT s.parent_region_id, s.region_id, c.cust_id, n.bo_name
FROM table

Using OWIN I have a startup.cs and I'm calling FluentMapper.Initialize() in there, like this:

public void Configuration(IAppBuilder app)
{
    var config = new HttpConfiguration();
    var builder = new ContainerBuilder();
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    builder.RegisterType<Repository>().AsImplementedInterfaces();

    var container = builder.Build();
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    app.UseAutofacMiddleware(container);
    app.UseAutofacWebApi(config);
    WebApiConfig.Register(config);
    app.UseWebApi(config);

    config
        .EnableSwagger(c => c.SingleApiVersion("v1", "House Accounts API! Welcome!"))
        .EnableSwaggerUi();

    // Map Dapper Query to POCO
    FluentMapper.Initialize(mapConfig =>
    {
        mapConfig.AddMap(new HouseAccountMap());
    });
}

My model gets serialized as all null values.

henkmollema commented 8 years ago

@richarddavenport how do you execute the query with Dapper?

richarddavenport commented 8 years ago

Like this:

db.Query<HouseAccount>(query.Sql, query.Params);
richarddavenport commented 8 years ago

So I tried using the generic query like this:

var data = db.Query(query.Sql, query.Params);

And I now get results! But now I'm not sure how to cast the dynamic list to an IEnumerable<HouseAccount>

I get a cast error when I use (IEnumerable<HouseAccount>)data

henkmollema commented 8 years ago

@richarddavenport in your HouseAccountMap you have this method:

public void ProductMap() { ... }

You probably copied the ProductMap from an example and added void to it. But that should be the constructor of the HouseAccountMap for the mapping to work, like this:

public class HouseAccountMap : EntityMap<HouseAccount>
{
    public HouseAccountMap()
    {
        // Define your mappings here
    }
}
henkmollema commented 7 years ago

Closing this for now. Please re-open if it's still an issue.