Many to many intermediate table foreign keys naming

67 views
Skip to first unread message

H.Alex

unread,
Jan 28, 2013, 3:18:37 AM1/28/13
to codec...@googlegroups.com
Hi!

I am using ConfORM in my new project at work, it's really a joy.
It is the request of the customer to name all foreign keys, so I have a thing like this:

    public class MyManyToManyApplier : IPatternApplier<MemberInfo, NHibernate.Mapping.ByCode.IManyToManyMapper>
    {
        public void Apply(MemberInfo subject, NHibernate.Mapping.ByCode.IManyToManyMapper applyTo)
        {
            var linkedTo = subject.GetPropertyOrFieldType().GetGenericArguments().First().Name;
            var fkName = "FK_" + subject.DeclaringType.Name + "_To_" + linkedTo + "_" + subject.Name;
 
            applyTo.ForeignKey(fkName);
        }
 
        public bool Match(MemberInfo subject)
        {
            return true;
        }
    }

But, how can I name the m2m intermediate tables? Is this even possible in NH? Without adding class mappings for them?

Thanks for your answers and thanks to Fabio for his great work.

Sincerely,
Alex

H.Alex

unread,
Jan 28, 2013, 6:35:02 AM1/28/13
to codec...@googlegroups.com
I was able to solve this by having the following in addition to my m2m applier:


        private void Mapper_AfterMapBag(IDomainInspector domainInspector, NHibernate.Mapping.ByCode.PropertyPath member, NHibernate.Mapping.ByCode.IBagPropertiesMapper propertyCustomizer)
        {
            var from = member.LocalMember.DeclaringType;
            var to = member.LocalMember.GetPropertyOrFieldType().GetGenericArguments().First();
 
            string fkName;
            
            if (domainInspector.IsManyToMany(from, to))
            {
                //map inverse fk names of m2m bags.
                fkName = "FK_" + to.Name + "_To_" + from.Name + "_" + member.ToColumnName();
            }
            else
            {
                //map inverse fk names of unidirectional one to many.
                fkName = "FK_" + from.Name + "_To_" + to.Name + "_" + member.ToColumnName();
            }
 
            propertyCustomizer.Key(km => km.ForeignKey(fkName));
        }


Thanks :)
Reply all
Reply to author
Forward
0 new messages