I have this error occurs when saving:
"object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave."
Entities:
public class Division : EntityBase<int>
{
public Division()
: base()
{
DivisionType = new DivisionType();
Employees = new List<Employee>();
DivisionChildren = new List<Division>();
}
public virtual string DivisionName { get; set; }
public virtual DivisionType DivisionType { get; set; }
public virtual Division ParentDivision { get; set; }
public virtual IList<Division> DivisionChildren { get; set; }
public virtual IList<Employee> Employees { get; set; }
}
Mappings:
public class DivisionMap : ClassMap<Division>
{
public DivisionMap()
{
Table("param.Division");
Id(x => x.Id).Column("DivisionId");
Map(x => x.DivisionName);
References(x => x.DivisionType).Column("DivisionTypeId");
References(x => x.ParentDivision).Column("ParentDivisionId");
HasMany(x => x.DivisionChildren).KeyColumn("ParentDivisionId").Inverse().Cascade.AllDeleteOrphan();
HasMany(x => x.Employees).KeyColumn("DivisionId").Inverse().Cascade.AllDeleteOrphan();
}
}
As you can see, I have Divison with its parent and childs. When I call:
Session.SaveOrUpdate(entity);
NhUnitOfWork.Current.Commit();
The error exception above occurs!!
Where the problem is?