理解会话.删除Fluent Nhibernate

本文关键字:Fluent Nhibernate 删除 会话 | 更新日期: 2023-09-27 18:12:40

假设我们有以下实体:

public class plaserv
{
    public virtual int id { get; private set; }
    [Required]
    public virtual DateTime fecha { get; set; }
    public virtual IList<plaattach> adjuntos { get; set; }
    public plaserv()
    {
        adjuntos = new List<plaattach>();
    }
}
public class plaattach
{
    public virtual int id { get; set; }
    public virtual plaserv plaserv { get; set; }
    [Required, StringLength(500)]
    public virtual string destination_name { get; set; }
}

我们也有以下几个存储库:

public class PlaServRepository
{
    private SessionHelper _sessionHelper;
    public PlaServRepository(SessionHelper sessionHelper)
    {
        _sessionHelper = sessionHelper;
    }
    public void Delete(plaserv entity)
    {
        using (ITransaction transaction = _sessionHelper.Session.BeginTransaction())
        {
            _sessionHelper.Session.Delete(entity);
            transaction.Commit();
        }
    }
    .....
}

public class PlaAttachRepository
{   
 ....
    public void Delete(plaattach entity)
    {
        using (ITransaction transaction = _sessionHelper.Session.BeginTransaction())
        {
            _sessionHelper.Session.Delete(entity);
            transaction.Commit();
        }
    }
}

此外,模型是使用自动映射创建的,因为您可以看到plaattach plaserv和一对多之间的关联。现在假设我想删除一个表行plattach,因为当你从存储库plattachrepository运行_sessionHelper.Session.Delete (entity)时不仅plattachrepository删除了表中的行而且plattachrepository还删除了plaserv中的行?

我希望解释正确

理解会话.删除Fluent Nhibernate

我真的很笨,而且显然非常新手,我的类真的实现了级联删除约定。当进行以下更改时,一切正常

    public void Apply(IManyToOneInstance instance)
    {
        instance.Cascade.None();
    }