I am using NHibernate 3.3.1 and found a very interesting problem. In my domain model, I defined two classes: Carriage and CarriageRequest, which reference each other. Since lazy loading is turned on when I access the caret. CarRequest - it points to a proxy object. This is normal. But when I call any method defined in the CarriageRequest class, 'this' refers to the third instance of the object.
Example:
class CarriageRequest
{
public virtual void Test(CarriageRequest instance)
{
Debug.WriteLine(Object.ReferenceEquals(this, instance));
}
}
class Carriage
{
public virtual CarriageRequest CarriageRequest { get; set; }
}
...
var carriage = session.Get<Carriage>(123);
carriage.CarriageRequest.Test(carriage.CarriageRequest);
So, it looks like NHibernate proxies are wrapping source objects and redirecting all method calls to wrapped objects. How can I use "==" in this case? I need to do something like this:
var shipment = (from sh in Carriage.Shipments where sh.CarriageRequest == this & sh.Warehouse == waybill.Warehouse select sh).FirstOrDefault();
-, "" , "sh.CarriageRequest == this" false. sh.CarriageRequest -, .