0

Im sorry if I don't make any sense my brain is fried right now. I just started programming in NHibernate, decided to move to Fluent Nhibernate because of the automapping but I'm having a little trouble, here is the relation it should have:

enter image description here

Here is the relation Fluent Nhibernate's automapping made, notice that MemberId is added to the table: enter image description here

I'm guessing this is because the relations are set to inverse in my conventions. I needed to make the relationships inverse() or the cascades didn't work. Here are my conventions:

public class HasManyConvention : IHasManyConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
        {
            instance.Key.Column(instance.EntityType.Name + "ID");
            //instance.Inverse();
            if (instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false).Length <= 0)
            {
                instance.Cascade.None();
                return;
            }
            var cascadeOption = (CascadeAttribute)instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false)[0];
            switch (cascadeOption.CascadeOption)
            {
                case Enums.CascadeOptions.All:
                    instance.Cascade.All();
                    break;

                case Enums.CascadeOptions.AllDeleteOrphan:
                    instance.Cascade.AllDeleteOrphan();
                    break;

                case Enums.CascadeOptions.Delete:
                    instance.Cascade.All();
                    break;

                case Enums.CascadeOptions.DeleteOrphan:
                    instance.Cascade.DeleteOrphan();
                    break;


                case Enums.CascadeOptions.None:
                    instance.Cascade.None();
                    break;

                case Enums.CascadeOptions.SaveUpdate:
                    instance.Cascade.SaveUpdate();
                    break;
            }

        }
    }



 public class ReferenceConvention : IReferenceConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
        {
            instance.Column(instance.Property.Name + "Fk");
            if (Attribute.IsDefined(instance.Property.PropertyType.Assembly, typeof(DomainSignatureAttribute)))
                instance.UniqueKey("DomainSignature");
            else
                instance.Index(instance.Property.Name + "Index");
        }
    }

And my two entities:

public class Member : Entity
    {
        public Member()
        {

            Mail = new List<Mail>();
        }


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



    }


public class Mail : Entity
    {

        public virtual Member Receiver
        {
            get; set;
        }

        public virtual Member Sender
        {
            get; set;
        }

        public virtual string Subject
        {
            get;
            set;
        }

        public virtual string Body
        {
            get; set;
        }
    }

Why does fluent nhibernate map MemberId when it's obvious that Receiver and Sender are foreign keys to the member table?

Daniel Schilling
  • 4,829
  • 28
  • 60
Eitan
  • 1,434
  • 6
  • 21
  • 53

1 Answers1

1

Because Member.Mail is mapped as one-to-many which results in a foreign key in Mail. Nobody ever said that 'Member.Mail' is an inverse relation and nobody ever said how the foreign key is called. So it creates a standard foreign key.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • @Eitan, he means that you never specified how that relationship should be mapped so it just assumes a standard foreign key which it calls MemberId by convention. – Vadim Mar 10 '11 at 15:21
  • @Eitan: Inverse means that the relation is redundant to another and doesn't need to be stored. It doesn't change the database model, but if you want to have bidirectional references, you should use inverse. – Stefan Steinegger Mar 10 '11 at 15:24