0

我有这两个课

[ActiveRecord("Customers", Lazy=true)]
public class Customer : ActiveRecordBase<Customer>
{
    [PrimaryKey(PrimaryKeyType.Identity, Access = PropertyAccess.AutomaticProperty)]
    public virtual int Id { get; private set; }

    [Property]
    public virtual string Name { get; set; }

    [HasMany(Lazy = true)]
    public virtual IList<Order> Orders { get; set; }
}

[ActiveRecord("Orders", Lazy=true)]
public class Order : ActiveRecordBase<Order>
{
    [PrimaryKey(PrimaryKeyType.Identity, Access = PropertyAccess.AutomaticProperty)]
    public virtual int Id { get; private set; }

    [Property]
    public virtual string Product { get; set; }

   [BelongsTo(Lazy = FetchWhen.OnInvoke)]
   public virtual Customer Customer { get; set; }
}

我想选择所有订单并为它们中的每一个,获取客户的 ID。所以我这样做:

using (new SessionScope())
{
   var orders = Order.FindAll();
   foreach (var o in orders)
   {
      int id = o.Customer.Id;
   }
}

它可以工作,但它会为每次调用 o.Customer.Id 创建一个 SELECT 短语。现在,订单已经有了客户的 ID,那么我怎样才能在不重新访问数据库的情况下获得它呢?

4

1 回答 1

0

以下代码应该允许您获取 id(但我目前无法对其进行测试):

int id = (int)((INHibernateProxy)order.Customer).HibernateLazyInitializer.Identifier;

问候

胡伊·尤卡

于 2013-06-27T17:19:48.997 回答