Ronnie Overby
Ronnie Overby

Reputation: 46470

nhibernate insert question - have id but not entity object

Using NHibernate I need to insert an entity into a database that has a child entity. Example:

public class Reservation
{
    public int Id { get; set; }
    public Service Service { get; set; }
}

public class Service
{
    public int Id { get; set; }
}

I need to create a new Reservation and insert it. However, when constructing the Reservation for insertion, I don't have the Service entity, but I do have the Service's Id value. Is there a way to insert my reservation without fetching the Service first?

Upvotes: 1

Views: 244

Answers (1)

PhilB
PhilB

Reputation: 11902

You can use NHibernate's Load method. This will create a proxy for the Service object, but it will not actually hit the database. See this blog post for the difference between load and get.

Upvotes: 5

Related Questions