How do I configure mappings for bidirectional mapping in the scenario of X references Y, Y has many Xs, so that when I add many Xs to the Y instance and save Y instance, it'll work properly?
to make the problem clear, here's the code:
ClientCompany model has HasMany
ContactPerson related models:
public class ClientCompany
{
// ....
public virtual ISet<ContactPerson> ContactPeople { get; set; }
}
// mapping:
HasMany(x => x.ContactPeople)
.AsSet()
.Inverse()
.Cascade.All();
the ContactPerson has not-nullable "ClientCompany" field referencing the parent company:
public class ContactPerson
{
// ....
public virtual ClientCompany ClientCompany { get; set; }
}
References(x => x.ClientCompany).Not.Nullable();
calling this code:
sess.Save(new ClientCompany()
{
ContactPeople = new HashSet<ContactPerson>()
{
new ContactPerson()
}
});
causes this exception:
not-null property references a null or transient value xxxx.ContactPerson.ClientCompany
this is just simplified case, I'm using AutoMapper in my real-world project so setting the reference manually is not the solution