开发者

NHibernate Many-to-one cascading delete (how to?)

开发者 https://www.devze.com 2023-01-16 14:53 出处:网络
I have this scenario: public class Survey : EntityBase { public virtual string Name { get; set; } } public class Response : EntityBase

I have this scenario:

public class Survey : EntityBase
{
    public virtual string Name { get; set; }
}

public class Response : EntityBase
{
    public virtual string Name { get; set; }
    public virtual Survey Survey { get; set; }
}

public class SurveyMap : ClassMap<Survey>
{
    开发者_Python百科public SurveyMap()
    {
        this.Id(e => e.Id);
        this.Map(e => e.Name);
    }
}

public class ResponseMap : ClassMap<Response>
{
    public ResponseMap()
    {
        this.Id(e => e.Id);
        this.Map(e => e.Name);
        this.References(e => e.Survey);         
    }
}

I want responses to be deleted automatically when I delete surveys, how to I configure this using Fluent NHibernate.

Thanks!


You should add a property to your Survey entity :

public class Survey : EntityBase 
{
    public virtual string Name { get; set; } 
    public IList<Response> Responses { get; set; }
}

And map it using cascade :

public class SurveyMap : ClassMap<Survey>
{
    public SurveyMap()
    {
        this.Id(e => e.Id);
        this.Map(e => e.Name);
        this.HasMany( e => e.Responses ).Inverse().Cascade().All();
    }
}


Try setting the reference to use the delete-orphan option.

NHibernate Cascades: the different between all, all-delete-orphans and save-update

0

精彩评论

暂无评论...
验证码 换一张
取 消