4

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 how to do so.

In the OrmLite examples that use the Northwind database, for example, would it be possible to return a Shipper object that included the "ShipperTypeName" as a string looked up through the foreign key "ShipperTypeID"?

From http://www.servicestack.net/docs/ormlite/ormlite-overview, I'd like to add the ShipperName field to the Shipper class if possible:

[Alias("Shippers")]
public class Shipper : IHasId<int>
{
    [AutoIncrement]
    [Alias("ShipperID")]
    public int Id { get; set; }

    [Required]
    [Index(Unique = true)]
    [StringLength(40)]
    public string CompanyName { get; set; }

    [StringLength(24)]
    public string Phone { get; set; }

    [References(typeof(ShipperType))]
    public int ShipperTypeId { get; set; }
}

[Alias("ShipperTypes")]
public class ShipperType : IHasId<int>
{
    [AutoIncrement]
    [Alias("ShipperTypeID")]
    public int Id { get; set; }

    [Required]
    [Index(Unique = true)]
    [StringLength(40)]
    public string Name { get; set; }
}
Dennis Ward
  • 747
  • 3
  • 8
  • 21

1 Answers1

10

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
    }
]
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks for the prompt reply! I appreciate your help (and your effort on servicestack - it's really nice to integrate with an MVC project. Do you have any plans of adding support for this type of thing with OrmLite? – Dennis Ward Dec 23 '11 at 19:51
  • Depends, What would the ideal API look like for this? (keeping the models as POCOs) – mythz Dec 23 '11 at 20:53
  • Not sure. Maybe something added to to the DTO either via an additional argument to the [References(type, foreigntype, foreigntypealias)] attribute that would allow definining a lookup via foreign key? I'm not sure if this goes beyond what a micro-orm should do, but it would be great if this facility were available. Thanks again for your quick response and have a merry Christmas! – Dennis Ward Dec 24 '11 at 03:04
  • Ok - after having used ServiceStack (and the MVC powerpack), I have a better idea about the micro-orm concept, and retract my request. I find that setting up stored procs and calling them from IDbCommand and using ConvertToList<> to be extremely simple and fast. – Dennis Ward Jan 02 '12 at 03:30
  • Cool. Also as they're just Ext methods on IDbCommand it's easy to add your own extensions to help with Data Access - and if they're generically useful to others we'd love to see it contributed back into the project. – mythz Jan 02 '12 at 05:09
  • I don't know how much polish it has had since I last looked but somebody was working on a feature for this: https://github.com/ServiceStack/ServiceStack.OrmLite/pull/120 https://github.com/ServiceStack/ServiceStack.OrmLite/pull/124 – DanB Feb 07 '13 at 17:00