Castle ActiveRecord 加入
我是 Castle ActiveRecord 新用户,现在我发现有必要执行涉及 JOIN 的选择。我不知道该怎么做。
这是我的两个(示例)类:
[ActiveRecord("Orders")]
public class Order : ActiveRecordBase<Order>
{
[PrimaryKey(PrimaryKeyType.Identity)]
public int OrderID { get; set; }
[Property]
public int UserID { get; set; }
[Property]
public DateTime OrderDate { get; set; }
[Property]
public string ShipName { get; set; }
}
[ActiveRecord("Users")]
public class UserAccount : ActiveRecordBase<UserAccount>
{
[PrimaryKey(PrimaryKeyType.Identity)]
public int UserID { get; set; }
[Property]
public string FirstName { get; set; }
[Property]
public string LastName { get; set; }
}
现在,如果我想获取所有订单以及 UserAccount.FirstName 和 UserAccount.LastName,我该怎么做?根据我的阅读,我需要创建另一个类,如“OrdersWithUserAccount”,但我不知道如何定义它。
感谢您帮助菜鸟!
I'm a new Castle ActiveRecord user, and I now find it necessary to do a select that involves a JOIN. I have no idea how to do so.
Here's my two (sample) classes:
[ActiveRecord("Orders")]
public class Order : ActiveRecordBase<Order>
{
[PrimaryKey(PrimaryKeyType.Identity)]
public int OrderID { get; set; }
[Property]
public int UserID { get; set; }
[Property]
public DateTime OrderDate { get; set; }
[Property]
public string ShipName { get; set; }
}
[ActiveRecord("Users")]
public class UserAccount : ActiveRecordBase<UserAccount>
{
[PrimaryKey(PrimaryKeyType.Identity)]
public int UserID { get; set; }
[Property]
public string FirstName { get; set; }
[Property]
public string LastName { get; set; }
}
Now if I want to get all orders, along with the UserAccount.FirstName and UserAccount.LastName, how do I do it? By my reading, I need to create another class like "OrdersWithUserAccount" but I have no idea how to define it.
Thanks for helping out a noob!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否已完成Castle ActiveRecord 教程?
我注意到的一件事是,您没有对象关系,而是关键关系。要按照 NHibernate 和 Castle ActiveRecord 的预期方式使用它们,您的对象模型应该具有对象之间的关系。
这意味着您的
Order
类应该有一个User
属性,该属性指向UserAccount
的实例。您的UserAccount
类应该有一个Orders
集合。根据我的经验,这通常应该是一个ISet
,您可以在UserAccount
构造函数中将其初始化为HashedSet
。Have you gone through the Castle ActiveRecord tutorial?
One thing I notice is that you don't have object relations, but key relations. To use NHibernate and Castle ActiveRecord the way they're intended, your object model should have relationships betweeen objects.
This means that your
Order
class should have aUser
property which points to an instance of aUserAccount
. YourUserAccount
class should have anOrders
collection. In my experience, this should usually be anISet<Order>
, which you initialize in theUserAccount
constructor as aHashedSet<Order>
.