我正在尝试跨越两个数据库表的简单选择。我正在从名为PlayerEquipment
的关联表中进行选择,如下所示:
PlayerId | ItemId | Quantity | IsEquipped
----------+--------+----------+------------
1 1 1 1
1 3 1 0
这构成了我们系统中玩家的广告资源,映射到Player
和Item
表格中的特定ID。
关联的PlayerEquipment
POCO如下所示:
public class PlayerEquipment
{
[Ignore]
public string Id
{
get { return PlayerId + "/" + ItemId; }
}
public int PlayerId { get; set; }
[References(typeof(ItemData))]
public int ItemId { get; set; }
public int Quantity { get; set; }
public bool IsEquipped { get; set; }
[Reference]
public ItemData ItemData { get; set; }
}
目前,忽略PlayerId
与相关Player
对象无关的事实(最终会是这样)。
我正在尝试获取一个玩家的设备列表,给出他的ID:
List<PlayerEquipment> equipment = this.Db.LoadSelect<PlayerEquipment>(q => q.PlayerId == playerId);
当我打电话时,我得到了这个例外:
Method not found: 'Boolean ServiceStack.EnumerableExtensions.IsEmpty(System.__Canon[])'.
...使用此堆栈跟踪:
at ServiceStack.OrmLite.OrmLiteReadCommandExtensions.LoadListWithReferences[Into,From](IDbCommand dbCmd, SqlExpression`1 expr, String[] include)
at ServiceStack.OrmLite.ReadExpressionCommandExtensions.LoadSelect[T](IDbCommand dbCmd, Expression`1 predicate, String[] include)
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.<>c__DisplayClass34`1.<LoadSelect>b__33(IDbCommand dbCmd)
at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func`2 filter)
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.LoadSelect[T](IDbConnection dbConn, Expression`1 predicate, String[] include)
[snip]
在线查看,我没有找到任何有关此异常的帮助,但我无法想象如何让我的查询更简单。我是以错误的方式解决这个问题吗?我应该改为查询引用Player
属性的Item
或List<PlayerEquipment>
对象吗?
答案 0 :(得分:0)
我刚刚在所有受支持的数据库中尝试过此示例,并且它按预期工作。唯一的变化是删除主键上不允许的[Ignore]
,因为它们需要存在于基础RDBMS表中。
public class PlayerEquipment
{
public string Id
{
get { return PlayerId + "/" + ItemId; }
}
public int PlayerId { get; set; }
[References(typeof(ItemData))]
public int ItemId { get; set; }
public int Quantity { get; set; }
public bool IsEquipped { get; set; }
[Reference]
public ItemData ItemData { get; set; }
}
public class ItemData
{
[AutoIncrement]
public int Id { get; set; }
public string Data { get; set; }
}
db.DropTable<PlayerEquipment>();
db.DropTable<ItemData>();
db.CreateTable<ItemData>();
db.CreateTable<PlayerEquipment>();
var item1 = new ItemData { Data = "ITEM1" };
db.Save(item1);
db.Save(new PlayerEquipment
{
PlayerId = 1,
ItemId = item1.Id,
Quantity = 1,
IsEquipped = true,
});
var item2 = new ItemData { Data = "ITEM2" };
db.Save(item2);
db.Save(new PlayerEquipment
{
PlayerId = 1,
ItemId = item2.Id,
Quantity = 1,
IsEquipped = false,
});
var playerId = 1;
var results = db.LoadSelect<PlayerEquipment>(q => q.PlayerId == playerId);
results.PrintDump();
[
{
Id: 1/1,
PlayerId: 1,
ItemId: 1,
Quantity: 1,
IsEquipped: True,
ItemData:
{
Id: 1,
Data: ITEM1
}
},
{
Id: 1/2,
PlayerId: 1,
ItemId: 2,
Quantity: 1,
IsEquipped: False,
ItemData:
{
Id: 2,
Data: ITEM2
}
}
]