user1394622
user1394622

Reputation: 67

Saving Parent Entity using Id of Entity than using whole entity

I have been using Nhibernate for a while but was wondering if thee was a way to save my child entity using just Id of the parent item not whole parent item.

Class Parent 
{
int Id {get;set;}
string name {get;set;}
}

Class Child
{
int Id {get;set;}
string name {get;set;}
Parent parent {get;set;}
}

Upvotes: 0

Views: 69

Answers (1)

jsur
jsur

Reputation: 46

You need to instantiate a Parent object. However, this doesn't mean you actually have to load a Parent record from the database. If you can use session.Load to instantiate the Parent object then, provided you have lazy loading enabled for your entities, this does nothing more than create a proxy object which holds the Id.

var c = new Child() {
  name = "Foo",
  parent = session.Load<Parent>(parentId)
};

session.Save(c);

Upvotes: 2

Related Questions