I have two entity classes: Person and Position.
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Position Position { get; set; }
}
public class Position
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
}
Person has a one-to-many relationship with Position. I also have a DTO PersonDto that brings the person data from the client.
public class PersonDto
{
public string Name { get; set; }
public int PositionId { get; set; }
}
After I get the DTO I have to create a new Person object and persist it. Usually it looks like this:
var person = new Person();
CopyFromDto(person, personDto);
var position = positionRepository.GetById(personDto.PositionId);
person.Position = position;
personRepository.Save(person);
I wonder if I could get rid of this line:
var position = positionRepository.GetById(personDto.PositionId);
The reason is that it produces an unneccessary SQL query that fetches some position data. Is it possible to somehow provide NHibernate with the position id and save a person row with it?
Thanks in advance.
Use
ISession.Load:https://nhibernate.info/doc/nh/en/index.html#manipulatingdata-loading