ServiceStack.OrmLite - how to include field from foreign key lookup?

后端 未结 1 745
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 06:57

I\'m trying out the ServiceStack MVC PowerPack, and am trying the included OrmLite ORM and am trying to get data from a table referenced by a foreign key without any idea ho

相关标签:
1条回答
  • 2020-12-09 07:31

    To do this you would need to use Raw SQL containing all the fields you want and create a new Model that matches the SQL, so for this example you would do something like:

    public class ShipperDetail
    {
        public int ShipperId { get; set; }
        public string CompanyName { get; set; }
        public string Phone { get; set; }
        public string ShipperTypeName { get; set; }
    }
    
    var rows = dbCmd.Select<ShipperDetail>(
        @"SELECT ShipperId, CompanyName, Phone, ST.Name as ShipperTypeName
            FROM Shippers S INNER JOIN ShipperTypes ST 
                     ON S.ShipperTypeId = ST.ShipperTypeId");
    
    Console.WriteLine(rows.Dump());
    

    Which would output the following:

    [
        {
            ShipperId: 2,
            CompanyName: Planes R Us,
            Phone: 555-PLANES,
            ShipperTypeName: Planes
        },
        {
            ShipperId: 3,
            CompanyName: We do everything!,
            Phone: 555-UNICORNS,
            ShipperTypeName: Planes
        },
        {
            ShipperId: 4,
            CompanyName: Trains R Us,
            Phone: 666-TRAINS,
            ShipperTypeName: Trains
        }
    ]
    
    0 讨论(0)
提交回复
热议问题