FluentNHibernate: Automapping OneToMany relation using attribute and convention

后端 未结 2 1153
挽巷
挽巷 2021-02-09 20:29

This is very similar to my previous question: FluentNHibernate: How to translate HasMany(x => x.Addresses).KeyColumn("PersonId") into automapping


Say

相关标签:
2条回答
  • 2021-02-09 21:12

    I've tested your classes with FHN's auto-mapping feature and it does not create that second PersonId on Address table. I'm using FHN v1.2.0.721 from here

    0 讨论(0)
  • 2021-02-09 21:26

    Sigh... Learning NHibernate is really a hair pulling experience.

    Anyway I think I finally figured out how to solve this problem: Just remove the SimpleForeignKeyConvention and everything will work fine.

    It seems the SimpleForeignKeyConvention conflicts with both ReferenceKeyConvention & KeyColumnConvention. It has higher priority than KeyColumnConvention but lower priority than ReferenceKeyConvention.

    public class SimpleForeignKeyConvention : ForeignKeyConvention
    {
        protected override string GetKeyName(Member property, Type type)
        {
            if(property == null)
                // This line will disable `KeyColumnConvention`
                return type.Name + "Id";
    
            // This line has no effect when `ReferenceKeyConvention` is enabled.
            return property.Name + "Id";
        }
    }
    
    0 讨论(0)
提交回复
热议问题