SlapperAutoMapper / Slapper.AutoMapper

Slapper.AutoMapper maps dynamic data to static types. Slap your data into submission!
MIT License
287 stars 76 forks source link

Deep nested classes #61

Closed john-aaron-owens closed 3 years ago

john-aaron-owens commented 6 years ago

Could someone take a look and tell me why Order.OrderDetails is returning as null after the map. The sql works and there is order details in the database. I can get nesting working for the 1 level, but not 2.

btw - I may have a typo as I had to rename the class and fields but the sql does work and no exceptions are being thrown. It just can't find the data which is nested a the 2nd level.

public class Project { public long ProjectId { get; set; } public IEnumerable Orders { get; set; } }

public class Order { public long OrderId { get; set; } public IEnumerable OrderDetails{ get; set; } }

public class OrderDetail { public long OrderDetailId { get; set; } }

        var sql = $@"

SELECT p.Project_ID AS ProjectId o.Order_ID AS Order_OrderId, od.Order_Detail_ID AS Order_OrderDetail_OrderDetailId, FROM Project AS p LEFT JOIN Order AS o ON p.Order_ID = o.Order_ID LEFT JOIN OrderDetail AS od ON o.Order_Detail_ID = od.Order_Detail_ID ";

        Slapper.AutoMapper.Cache.ClearInstanceCache();

        Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(ProjectSampleSetLegacyPortal),
            new List<string> { "ProjectId" });
        Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(StatsLegacyPortal),
            new List<string> { "OrderId" });
        Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(StatsDetailLegacyPortal),
            new List<string> { "OrderDetailId" });

        var results =
            Slapper.AutoMapper.MapDynamic<Project>(query).First();
randyburden commented 6 years ago

The column alias in your SQL query is not correct. You need to change from “Order_OrderId” to “Orders_OrderId”. Essentially you need to make the alias match the name of the property name on your object which is plural “Orders”.