理解Session.Delete Fluent Nhibernate

时间:2011-10-29 23:34:33

标签: c# nhibernate fluent-nhibernate

假设我们有以下实体:

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之间的关联是一对多。现在假设我要删除一个表行plaattach,因为当您从存储库PlaattachRepository运行_sessionHelper.Session.Delete (entity)时,不仅PlaattachRepository在表中删除了行而且还在plaserv行中进行了plaattach?

我希望能正确解释

1 个答案:

答案 0 :(得分:1)

我真的很傻,显然非常新秀,我的班级真的实现了级联删除约定。 进行以下更改时,一切正常

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