流畅的 Nhibernate Automap 一对多孤立记录
我的问题是,当我从一对多关系中删除一个对象时,子记录会被孤立而不是被删除。我不确定这是否是我设置域模型的方式,或者我在自动地图配置期间没有设置某些内容。评估-> ShortlistedMentor 关系是孤立记录发生的地方。它们同时出现在 ShortlistMentor
表和 ShortListQuestionResponse
中。我期望的是,当我从关系中删除 ShortlistMentor
时,它会从 ShortlistMentor 表中删除,并且 ShortListQuestionResponse 表中的条目也会被删除。
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; }
}
设置
.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>()});
}
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>()});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为由于您的 DefaultCascade 设置为 SaveUpdate(),您需要将 HasMany 关系 (ShortlistedMentors) 覆盖为 Cascade.AllDeleteOrphan。所以它看起来像这样:
我实际上没有编译它,所以它可能并不完美。
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:
I didn't actually compile this so it may not be perfect.