Fluent NHibernate DuplicateMappingException with AutoMapping

后端 未结 6 1667
终归单人心
终归单人心 2021-01-03 06:08

Summary:

I want to save two classes of the same name and different namespaces with the Fluent NHibernate Automapper

Context

6条回答
  •  时光说笑
    2021-01-03 06:42

    I had to play around with where to add the convention AutoImport.Never() to. I have my persistence mapping separated into different projects - models for each application can also be found in different projects. Using it with Fluent NHibernate and auto mapping.

    There are occasions when domains, well mappings really have to be combined. This would be when I need access to all domains. POCO classes used will sometimes have the same name and different namespaces, just as examples above.

    Here is how my combine all mapping looks like:

    internal static class NHIbernateUtility
    {
        public static ISessionFactory CreateSessionFactory(string connectionString)
        {
            return Fluently.Configure()
                .Database(
                    MsSqlConfiguration
                        .MsSql2008
                        .ConnectionString(connectionString))
                .Mappings(m => m.AutoMappings
                    .Add(ProjectA.NHibernate.PersistenceMapper.CreatePersistenceModel()))
                .Mappings(m => m.AutoMappings
                    .Add(ProjectB.NHibernate.PersistenceMapper.CreatePersistenceModel()))
                .Mappings(m => m.AutoMappings
                    .Add(ProjectC.NHibernate.PersistenceMapper.CreatePersistenceModel())).BuildSessionFactory();
        }
    }
    

    And one of the persistence mappers:

    public static class PersistenceMapper
    {
        public static AutoPersistenceModel CreatePersistenceModel()
        {
            return
                AutoMap.AssemblyOf(new AutoMapConfiguration())
                    .IgnoreBase()
                    .Conventions.Add(AutoImport.Never())
                    .Conventions.Add()
                    .Conventions.Add()
                    .Conventions.Add()
                    .Conventions.Add()
                    .Conventions.Add();
        }
    }
    

    Persistence mappers are very similar for each POCO namespace - some have overrides. I had to add .Conventions.Add(AutoImport.Never()) to each persistence mapper and it works like a charm.

    Just wanted to share this if anyone else is doing it this way.

提交回复
热议问题