Is there any way in NHibernate that I can use the following Entities
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Pet> Pets { get; set; }
}
public class Pet
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
And not have to create a "special" AddPet method on Person in order to have Child pets saved.
public void AddPet(Pet p)
{
p.Person = this;
Pets.Add(p);
}
_session.SaveOrUpdate(person);
Does not save the Pets because Pet has no Person reference.
If I update Pets to contain this reference.
public class Pet
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Person Person { get; set; }
}
On new pets I still have to set Person this seems like overkill to me and also risky as People can still call
person.Pets.Add(new Pet())
The only other option I can think of is a Custom list that sets the parent reference when adding child entities.