开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜