1
我的問題是,當我從一對多的關係中刪除一個對象的子記錄得到孤立而不是刪除。我不確定這是否是我設置域模型的方式,或者我沒有在自動映射配置期間設置某些東西。評估 - >短名單指導關係是發生孤兒記錄的地方。它們出現在ShortlistMentor
表和ShortListQuestionResponse
中。我期望的是,當我從關係中刪除ShortlistMentor
時,它將從ShortlistMentor表中刪除,並且ShortListQuestionResponse表中的條目也將被刪除。流利Nhibernate自動地圖一對多的孤兒記錄
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; }
}
自動地圖設置
.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())
)
不知道這是否幫助,但我就是這樣我從集合中移除的項目,增加新的
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>()});
}
我曾嘗試過,但我甚至嘗試將條目添加到SHortlistedMentors集合時遇到以下錯誤 「擁有級聯=」all-delete-orphan「的集合不再由擁有的實體實例引用: Poco.Appraisal.ShortlistedMentors「 – Simon 2011-04-05 13:26:02
此錯誤消息可能意味着您正在覆蓋您不應該在某處的集合參考。請參閱這兩篇文章,看看他們是否可以爲您解決這個問題:[here](http://stackoverflow.com/questions/2127016/nhibernate-mapping-a-collection-with-cascade-all-delete-orphan -was-no-longer-r)和[here](http://www.sleberknight.com/blog/sleberkn/entry/20070329) – 2011-04-05 15:05:53
我在發佈該文章之前閱讀過第一篇文章,並且正在做使用清除Collection來建議使用散列。清除()並再次添加任何新元素,但仍然出現錯誤。 – Simon 2011-04-06 10:15:07