0
votes

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?

1

1 Answers

1
votes

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.