ServiceStack.OrmLite - 如何从外键查找中包含字段? [英] ServiceStack.OrmLite - how to include field from foreign key lookup?
问题描述
我正在试用 ServiceStack MVC PowerPack,正在试用包含的 OrmLite ORM,正在尝试从外键引用的表中获取数据,但不知道如何操作.
例如,在使用 Northwind 数据库的 OrmLite 示例中,是否可以返回包含ShipperTypeName"的 Shipper 对象作为通过外键ShipperTypeID"查找的字符串?
来自 http://www.servicestack.net/docs/ormlite/ormlite-overview,如果可能,我想将 ShipperName 字段添加到 Shipper 类:
[别名(托运人")]公共类托运人:IHasId{[自动递增][别名(ShipperID")]公共 int Id { 获取;放;}[必需的][索引(唯一=真)][字符串长度(40)]公共字符串 CompanyName { 获取;放;}[字符串长度(24)]公共字符串电话{得到;放;}[参考文献(typeof(ShipperType))]公共 int ShipperTypeId { 获取;放;}}[别名("ShipperTypes")]公共类 ShipperType : IHasId{[自动递增][别名("ShipperTypeID")]公共 int Id { 获取;放;}[必需的][索引(唯一=真)][字符串长度(40)]公共字符串名称 { 获取;放;}}
为此,您需要使用包含您想要的所有字段的原始 SQL 并创建一个与 SQL 匹配的新模型,因此对于此示例,您将执行类似的东西:
公共类 ShipperDetail{公共 int ShipperId { 获取;放;}公共字符串 CompanyName { 获取;放;}公共字符串电话{得到;放;}公共字符串 ShipperTypeName { 获取;放;}}var rows = dbCmd.Select(@"SELECT ShipperId, CompanyName, Phone, ST.Name as ShipperTypeNameFROM Shippers S INNER JOIN ShipperTypes STON S.ShipperTypeId = ST.ShipperTypeId");Console.WriteLine(rows.Dump());
将输出以下内容:
<预><代码>[{ShipperId: 2,公司名称:Planes R Us,电话:555-PLANES,托运人类型名称:飞机},{ShipperId: 3,公司名称:我们无所不能!,电话:555-独角兽,托运人类型名称:飞机},{ShipperId: 4,公司名称:Trains R Us,电话:666-TRAINS,托运人类型名称:火车}]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; }
}
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
}
]
这篇关于ServiceStack.OrmLite - 如何从外键查找中包含字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!