[fluent-nhib] Cascade.AllDeleteOrphan() only clears foreign key

1,616 views
Skip to first unread message

RenzoV

unread,
May 19, 2010, 5:34:01 PM5/19/10
to Fluent NHibernate
I have a feeling this is quite simpel but i can't figure out what i'm
doing wrong. I got a simpel one to many relationship. Here are the
mappings:

public class PostMap : ClassMap<Post>
{
public PostMap()
{
Id(x => x.Id);
Map(x => x.Title);
Map(x => x.Content);
Map(x => x.PostDate);
HasMany(x => x.Comments).Cascade.AllDeleteOrphan();
}
}

public class CommentMap : ClassMap<Comment>
{
public CommentMap()
{
Id(x => x.Id);
Map(x => x.Text);
References(x => x.Post);
}
}

When i delete a Post it sets the Post_id in the Comments table to
null. I want it to delete the entire record. I've tried all cascade
options but nothing has the desired effect.

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

Craig Gardiner

unread,
May 19, 2010, 6:21:15 PM5/19/10
to fluent-n...@googlegroups.com
How are you deleting the 'Comment'?
From the parent (Post).
Have you tried specifying the Inverse property on the relationship.
I always to include this, maybe that will help.

RenzoV

unread,
May 19, 2010, 6:30:57 PM5/19/10
to Fluent NHibernate
I'm just deleting a Post. I don't touch the Comments themself, i want
them deleted if i delete a Post.

I have tried the Inverse() property. But if i then delete a Post i get
the following exception:
{"The DELETE statement conflicted with the REFERENCE constraint
\"FKE2466703A3669EE7\". The conflict occurred in database \"Blog\",
table \"dbo.Comment\", column 'Post_id'.\r\nThe statement has been
terminated."}

I'm not entirely clear what the Inverse property does yet. But i'm
guessing NHibernate tries to delete the Post (because Comment is de
Parent now) which fails because it still has Comments.

I don't care about an extra UPDATE being performed so i rather don't
have Inverse anyway because i think its more logical to have Post as
the Parent.
> For more options, visit this group athttp://groups.google.com/group/fluent-nhibernate?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
> To post to this group, send email to fluent-n...@googlegroups.com.
> To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/fluent-nhibernate?hl=en.

RenzoV

unread,
May 21, 2010, 1:22:20 PM5/21/10
to Fluent NHibernate
I'm still struggeling with this issue. It seems such a basic thing to
me.. Maybe it will help if i post all the code.

Entities:

using System;

namespace Blog.Logic
{
public class Comment
{
public Comment() { }

public virtual Int32 Id { get; private set; }
public virtual String Text { get; set; }
public virtual Post Post { get; set; }
}
}

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Blog.Logic
{
public class Post
{
public Post()
{
Comments = new Collection<Comment>();
}

public virtual void AddComment(Comment c)
{
c.Post = this;
Comments.Add(c);
}

public virtual Int32 Id { get; private set; }
public virtual String Title { get; set; }
public virtual String Content { get; set; }
public virtual DateTime PostDate { get; set; }
public virtual ICollection<Comment> Comments { get; private
set; }
}
}


Mapping files:

using FluentNHibernate.Mapping;

namespace Blog.Logic
{
public class CommentMap : ClassMap<Comment>
{
public CommentMap()
{
Id(x => x.Id);
Map(x => x.Text);
References(x => x.Post);
}
}
}

using FluentNHibernate.Mapping;

namespace Blog.Logic
{
public class PostMap : ClassMap<Post>
{
public PostMap()
{
Id(x => x.Id);
Map(x => x.Title);
Map(x => x.Content);
Map(x => x.PostDate);
HasMany(x => x.Comments).Cascade.AllDeleteOrphan();
}
}
}

How i build the session factory and created the database (i comment
the database part after i generated it):

Configuration cfg = new Configuration();
cfg.SetProperty(NHibernate.Cfg.Environment.Dialect,
"NHibernate.Dialect.MsSql2005Dialect");

cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider,
"NHibernate.Connection.DriverConnectionProvider");

cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionDriver,
"NHibernate.Driver.SqlClientDriver");

cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, "Server=.\
\SQLEXPRESS; Database=Blog;Integrated Security=SSPI;");
cfg.SetProperty(NHibernate.Cfg.Environment.Isolation,
"ReadCommitted");

cfg.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass,
"managed_web");
cfg.SetProperty(NHibernate.Cfg.Environment.DefaultSchema,
"Blog.dbo");
cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql,
"true");

cfg.SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass,
"NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle");
cfg.AddAssembly("Blog.Logic");

sessionFactory = Fluently
.Configure(cfg)
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<SessionManager>()
).BuildSessionFactory();

// Create Database
SchemaExport schema = new SchemaExport(cfg);
schema.Drop(false, true);
schema.Create(false, true);

How i delete the Comment:

sessionManager.sessionFactory.GetCurrentSession().Delete(entity);

cliff vaughn

unread,
May 22, 2010, 11:49:45 AM5/22/10
to fluent-n...@googlegroups.com
On Fri, May 21, 2010 at 1:22 PM, RenzoV <renzove...@gmail.com> wrote:

How i delete the Comment:

sessionManager.sessionFactory.GetCurrentSession().Delete(entity);


based on my experience, that is not the way to delete entities.  You should simply remove the comment object from the list that the blog maintains, save the blog, and that will cause the comment to be deleted.

--
thanks

cliff

RenzoV

unread,
May 22, 2010, 5:41:58 PM5/22/10
to Fluent NHibernate
I made a mistake in my previous post. Thats not how i delete a
Comment, thats how i delete a Post. It deletes the post just fine like
that (while else would that function be there). I'm pretty sure your
solution works for Comments aswell. But thats beside the point. I
don't want to touch the Comments at all. I just want them to get
deleted if i delete the Post it belongs to. Which to my knowledge is
exactly what Cascade on Delete is.

On May 22, 5:49 pm, cliff vaughn <cliftonfvau...@gmail.com> wrote:

Eluander Lopes

unread,
Apr 4, 2019, 3:21:35 PM4/4/19
to Fluent NHibernate
I have the same problem, did you find any solution?


Em quarta-feira, 19 de maio de 2010 18:34:01 UTC-3, RenzoV escreveu:
I have a feeling this is quite simpel but i can't figure out what i'm
doing wrong. I got a simpel one to many relationship. Here are the
mappings:

    public class PostMap : ClassMap<Post>
    {
        public PostMap()
        {
            Id(x => x.Id);
            Map(x => x.Title);
            Map(x => x.Content);
            Map(x => x.PostDate);
            HasMany(x => x.Comments).Cascade.AllDeleteOrphan();
        }
    }

    public class CommentMap : ClassMap<Comment>
    {
        public CommentMap()
        {
            Id(x => x.Id);
            Map(x => x.Text);
            References(x => x.Post);
        }
    }

When i delete a Post it sets the Post_id in the Comments table to
null. I want it to delete the entire record. I've tried all cascade
options but nothing has the desired effect.

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-n...@googlegroups.com.

Felipe Oriani

unread,
Apr 5, 2019, 9:53:05 AM4/5/19
to fluent-n...@googlegroups.com
Is there any rule defined for this FK on the database level? I mean, maybe its cascade is setted to "set null". 

Take a look at this link:

 



To unsubscribe from this group and stop receiving emails from it, send an email to fluent-nhibern...@googlegroups.com.

To post to this group, send email to fluent-n...@googlegroups.com.


--
______________________________________
Felipe B Oriani
Reply all
Reply to author
Forward
0 new messages