Is there a way in NHibernate so that I can use the following objects
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 you don’t need to create a “special” AddPet method for Person to have pet children.
public void AddPet(Pet p)
{
p.Person = this;
Pets.Add(p);
}
_session.SaveOrUpdate(person);
It doesn’t save pets, because Pet has no reference to humans.
If I update "Home" to contain this link.
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 need to establish a Personality, it seems unnecessary for me, and also risky, because people can still call
person.Pets.Add(new Pet())
The only other possibility I can think of is a custom list that sets the parent link when adding child objects.
source
share