0

I have what seems to be a very simple question but I can't seem to figure it out. If I have a one to many relationship between [Member] and [Mail]

If I take off inverse it all works, it's just when I call instance.inverse that it doesnt save.

public class Mail : Entity
    {
        public virtual Member Receiver
        {
            get; set;
        }

        public virtual string Subject
        {
            get;
            set;
        }

        public virtual string Body
        {
            get; set;
        }
    }



public class Member : Entity
    {
        public Member()
        {           
            Mail = new List<Mail>();            
        }

        [Cascade(Enums.CascadeOptions.All)]
        public virtual IList<Mail> Mail
        {
            get; set;
        }
    }

Inverse is true on a one to many (through an automapping convention)

public class HasManyConvention : IHasManyConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
        {

            instance.Key.Column(instance.EntityType.Name + "ID");
            instance.Inverse();
}}

I try to add a mail item to a member through:

mail.Receiver = receiver;
        receiver.Mail.Add(mail);           
        _repository.SaveOrUpdate(mail);
        FlushSessionAndEvict(mail);

Now what's weird is the mail item is saved to the database, there is a foreign key called 'MemberId' which SHOULD have the foreign key to the parent member table and a column called ReceiverFk which is according to my convention but shouldn't be there if there is already a foreign key relationship with MemberId. When I retrieve the user it doesn't have the mail item attached to it. If I take off inverse everything works, the foregin key is saved in MemberId.

I don't want to take off inverse though because it entails another update call to the database. What can I do?

Thanks

Eitan
  • 1,434
  • 6
  • 21
  • 53

2 Answers2

2

Inverse = true means "ignore this relationship because I'm saving it from the other side". So you need to cascade the save on the collection side as you are attempting to do. I suspect that the problem is that you're using Fluent NHibernate for the mapping, but the cascade is annotated via NHibernate mapping attributes. Fluent NHibernate doesn't read the attributes as far as I know. So you need some code to mix NHibernate mapping attributes into the Configuration along with Fluent NHibernate or use an explicit ClassMap or Fluent NHibernate convention to override the cascade relationship on the Member.Mail collection.

James Kovacs
  • 11,549
  • 40
  • 44
  • The attribute is something I added to tell the automapper to cascade in all my relationship conventions I have a switch (cascadeOption.CascadeOption) and it works in regards to the cascading – Eitan Mar 16 '11 at 06:57
1

Try adding Cascade.All to your mapping, it normally solves most of my NHibernate problems =)

jonnii
  • 28,019
  • 8
  • 80
  • 108