1

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>()});

            }
Simon
  • 415
  • 1
  • 4
  • 15

1 Answers1

1

I think since your DefaultCascade is set to SaveUpdate() you'll need to override your HasMany relationship (ShortlistedMentors) to a Cascade.AllDeleteOrphan. So it would look something like this:

.Override<Appraisal>(map =>{map.HasMany(x => x.ShortlistedMentors).Cascade.AllDeleteOrphan();})

I didn't actually compile this so it may not be perfect.

Cole W
  • 15,123
  • 6
  • 51
  • 85
  • I had tried that but I get the following error when I even try to add an entry to the SHortlistedMentors collection "A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Poco.Appraisal.ShortlistedMentors" – Simon Apr 05 '11 at 13:26
  • This error message may mean that you are overwriting your collection reference somewhere you shouldn't be. Please refer to these 2 articles to see if they can fix this issue for you: [here](http://stackoverflow.com/questions/2127016/nhibernate-mapping-a-collection-with-cascade-all-delete-orphan-was-no-longer-r) and [here](http://www.sleberknight.com/blog/sleberkn/entry/20070329) – Cole W Apr 05 '11 at 15:05
  • I had read the first article before I posted this, and I'm doing what hash suggested by clearing out the Collection using . Clear() and adding in any new elements again, but I still get the error. – Simon Apr 06 '11 at 10:15
  • I think I may need to see your unit test for this. Also you may want to post this under a new issue with your modified mappings. – Cole W Apr 06 '11 at 14:24
  • I'm not unit testing this project, I know it something I should be doing but this project as about learning Nhib + Fluent + CastleWindsor I didn't want to though another concept in there until I have grasped the initial stack. I'll create another question with the updated detail in it, thanks so far Cole – Simon Apr 06 '11 at 16:05
  • No problem. It doesn't have to be a unit test. Just the code you are using to add the new elements to your has many and the code you are using to persist it. – Cole W Apr 07 '11 at 00:47