Re: Map foreign key as a property as well as reference to parent object.

26 views
Skip to first unread message

Ricardo Peres

unread,
Sep 19, 2012, 3:41:40 AM9/19/12
to nhu...@googlegroups.com
If you use stateless sessions you can work totally disconnected:

ChildEntity.ParentEntity = new ParentEntity(){ Id = 100 };

This is mostly the same as using ISession.Load(), because it won't go to the database (if your entity is configured as lazy, that is).

RP

On Monday, September 17, 2012 4:24:47 PM UTC+1, Mike B wrote:
Hello,

What I need is to be able to map both foreign key and a reference to a parent object. For example:


class Parent
     int                ParentID
     List<Child>   Children
 

class Child
    int         ChildID
    int         ParentID // foreign key
    Parent   ParentEntity 
 
class ParentMap
    HasMany(x => x.Children)
          .KeyColumn("ParentID ")   // PROBLEM!
          .Inverse()
          .Cascade.All();

class ChildMap
    Map(x => x.ParentID);     // PROBLEM!
    References(x => ParentEntity);

The reason behind this is that operations are done in a detached environment, and the detached environment doesn't "know" about NHibernate, so solutions like ChildEntity.ParentEntity = Sessions.Load(parentEntityID) are not acceptable. We explicitly need to work with the foreign key as a property.  Any help very appreciated

Reply all
Reply to author
Forward
0 new messages