虽然删除和添加工作正常,但当我更新子实体的父集合时,子记录上的外键只是设置为空。我希望它们从数据库中完全删除。
所以我一直在尝试 Cascade.All
、Cascade.DeleteOrphans
、Cascade.All.Include(Cascade.DeleteOrphans)
,但似乎没有任何效果。
如果我在父级上将 Inverse 设置为 true 但它会导致子记录根本没有更新。
这是我的代码:
Id(e => e.Id, mapper => mapper.Generator(Generators.HighLow));
Property(e => e.Name);
Bag(e => e.Actors,
mapper =>
{
mapper.Key(k => k.Column("Movie_Id"));
mapper.Cascade(Cascade.All.Include(Cascade.DeleteOrphans));
},
relation => relation.OneToMany(
mapping => mapping.Class(typeof(Actor))));
Id(e => e.Id, mapper => mapper.Generator(Generators.HighLow));
Property(e => e.FirstName);
public class Movie : EntityBase
{
public virtual string Name { get; set; }
public virtual IEnumerable<Actor> Actors { get; set; }
}
public void Update(T entity)
{
using (var transaction = Session.BeginTransaction())
{
try
{
Session.Update(entity)
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
答案 0 :(得分:0)
将 Update() 更改为 Merge() 对我有用。