My problem is that when I remove an object from the one to many relationship the child records get orphaned as opposed to deleted. I'm not sure if it the way I have set up my domain model or I'm not setting something up during the auto map configuration. The Appraisal -> ShortlistedMentor Relationship is where the orphaned records are occurring. They occur both in the ShortlistMentor
table and the ShortListQuestionResponse
. What I expect is that when I remove a ShortlistMentor
from the relationship is that it gets removed from the ShortlistMentor table and also the entries in ShortListQuestionResponse table also get removed.
public class Appraisal : BaseEntity
{
public Appraisal()
{
ShortlistedMentors = new List<ShortlistedMentor>();
ApprovedMentor = new User();
College = new RefData();
}
#region Primitive Properties
public virtual bool Decision { get; set; }
public virtual System.DateTime? ApprovedDate { get; set; }
public virtual System.DateTime? AcceptedDate { get; set; }
public virtual System.DateTime? CompletionTargetDate { get; set; }
public virtual string RejectionReason { get; set; }
public virtual IList<ShortlistedMentor> ShortlistedMentors { get; set; }
public virtual User ApprovedMentor { get; set; }
public virtual RefData College { get; set; }
}
public class ShortlistedMentor : BaseEntity
{
public virtual User Mentor { get; set; }
public virtual IList<ShortListQuestionResponse> ShortListQuestionResponses { get; set; }
}
public class ShortListQuestionResponse : BaseEntity
{
public virtual string Comment { get; set; }
public virtual int Score { get; set; }
public virtual RefData Question { get; set; }
}
Auto Map Set up
.Mappings
(
m =>
m.AutoMappings.Add
(
AutoMap.AssemblyOf<User>(cfg)
.Override<Client>(map =>{map.HasManyToMany(x => x.SICCodes).Table("SICRefDataToClient");})
.IgnoreBase<BaseEntity>()
.Conventions.Add(new StringColumnLengthConvention(),new EnumConvention(),DefaultCascade.SaveUpdate())
.Conventions.Add(DefaultLazy.Always())
)
not sure if this help but This is how I'm removing the items from the collection and adding new
ProjectToUpdate.Appraisal.ShortlistedMentors.Clear();
foreach (var userId in Request.Form["ProjectToEdit.Appraisal.ShortlistedMentors"].Split(','))
{
var user = _membershipService.GetUser(Convert.ToInt16(userId));
ProjectToUpdate.Appraisal.ShortlistedMentors.Add(new ShortlistedMentor(){Mentor = user,ShortListQuestionResponses = new List<ShortListQuestionResponse>()});
}