I have the following code which uses the repository pattern, backed by a fluent nhibernate automapping to a MySQL DB.
The following snippet adds a user, and a person to the model, and then persists to the database.
It works, however I have to remember to put user.Person.User = user;
otherwise when the object is persisted, Person
has the FK userId set to null.
Is there a way to automatically cause the
Person.User
to be populated, or do I need to create a method to do this?Is there a way to cause all objects associated with that user to be persisted (e.g. only call repository.Save(obj) on one object and cause the others to also be persisted.)
using (MySQLRepositoryBase repository = new MySQLRepositoryBase()) { try { repository.BeginTransaction(); User user = new User() { Password = "12345", Username = "jbloggs", Person = new Person() }; user.Person.Firstname = "Joe"; user.Person.Lastname = "Bloggs"; user.Person.User = user; repository.Save(user); repository.Save(user.Person); } catch (Exception ex) { MessageBox.Show(ex.ToString()); repository.RollbackTransaction(); } }