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);




On May 20, 12:30 am, RenzoV <renzoversch...@gmail.com> wrote:
> 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.
>
> On May 20, 12:21 am, Craig Gardiner <crgsoftw...@gmail.com> wrote:
>
>
>
>
>
> > 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.
>
> > -----Original Message-----
> > From: fluent-nhibernate@googlegroups.com
>
> > [mailto:fluent-nhibern...@googlegroups.com] On Behalf Of RenzoV
> > Sent: Thursday, 20 May 2010 7:34 AM
> > To: Fluent NHibernate
> > Subject: [fluent-nhib] Cascade.AllDeleteOrphan() only clears foreign key
>
> > 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-nhibern...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > fluent-nhibernate+unsubscr...@googlegroups.com.
> > 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-nhibern...@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > fluent-nhibernate+unsubscr...@googlegroups.com.
> > 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-nhibern...@googlegroups.com.
> To unsubscribe from this group, send email to 
> fluent-nhibernate+unsubscr...@googlegroups.com.
> 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-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to