我正在试图弄清楚如何连接两个表(Node和NodeDetails),这两个表有一个外键关联它们。基本上我正在寻找这个SQL查询的nhibernate linq版本:
SELECT node.ObjectType, nodeDetails.Name, nodeDetails.SizeInFeet
FROM node
left join nodeDetails
on node.ID = nodedetails.nodeID
ORDER BY node.ObjectType;
由于我正在进行连接而且我不需要所有列,因此我创建了另一个名为NodeDetailsView的实体,它假设保存结果。我的C#代码如下所示:
Node n = null;
NodeDetails nd = null;
var q = _session.QueryOver<NodeDetails>(() => nd)
.JoinQueryOver(x => x.NodeID, () => n)
.TransformUsing(Transformers.AliasToBean<NodeDetailsView>());
IList<NodeDetailsView> r = t.List<NodeDetailsView>();
我最终得到了这个例外: {“'字段列表'中的未知列'this_.SizeInFeet'”} 我不认为上面的linq查询是正确的,我的映射可能不正确。你们可以看看它并给我一些指示吗?谢谢!
我的实体课程: Node.cs
public class Node
{
public virtual int ID { get; set; }
public virtual string ObjectType { get; set; }
public virtual string Location {get; set;}
}
Nodetail.cs
public class NodeDetails : IVersionedEntity
{
public virtual int ID { get; set; }
public virtual Node NodeID { get; set; }
public virtual int SizeInFeet { get; set; }
public virtual float Price { get; set; }
}
视图类NodeDetailsView.cs
public class NodeDetailsView
{
public virtual string ObjectType { get; set; }
public virtual string SizeInFeet { get; set; }
public virtual int TotalSpace { get; set; }
}
Fluent Mappings
public class NodeMap: ClassMap<Node>
{
public WorldObjectMap()
{
Id(x => x.ID);
Map(x => x.ObjectType).Not.Nullable();
Map(x => x.Location).Not.Nullable();
}
}
public class NodDetailsMap : ClassMap<NodeDetails>
{
public NodDetailsMap()
{
Id(x => x.ID);
References(x => x.NodeID).Column("ID");
Map(x => x.MarketPrice).Nullable();
Map(x => x.SizeInFeet).Nullable();
}
}
public class NodeDetailsViewMapping : ClassMap<NodeDetailsView>
{
public WorldObjectLeasesViewMapping()
{
Map(x => x.ObjectType).Not.Nullable();
Map(x => x.Name).Nullable();
Map(x => x.SizeInFeet);
}
}
答案 0 :(得分:1)
老实说,我会使用Linq提供程序而不是QueryOver。如果你这样做会看起来像这样:
while StrLevel != "low" or "medium" or "high":
StrLevel = input("Please enter low, medium, or high for the program to work; ")
无需映射using NHibernate.Linq
IList<NodeDetailsView> r = _session.Query<NodeDetails>()
.Select(x => new NodeDetailsView
{
ObjectType = x.NodeID.ObjectType,
SizeInFeet = x.SizeInFeet,
TotalSpace = "not sure what this is..."
}
.ToList()
,因为这似乎是DTO /模型。您只映射代表数据库表或视图的对象。
从上面看,NodeDetailsView
因对象而异。一个是SizeInFeet
,另一个是string
。如果确实如此,您必须在查询中进行转换,例如int