0

I am new to fluent hibernate and I am getting an exception while trying to create a filter.

I found this Syntax to define a NHibernate Filter with Fluent Nhibernate?

which explains how to create a fluent filter all good until I try to run my application. Which I get an exception (see below). My code is identical to the link posted above. Here is the code that causes the exception. The problem disappears if I add only 1 assembly for mappings e.g. HotelMap, the moment I add 2 or more I get the exception.

Any thought it will be appreciated.

private static ISessionFactory BuildSessionFactory()
    {
        var connectionString = DatabaseConnectionStringsFactory.GetConnectionString(DatabaseConnectionStringsFactory.DEFAULT);
        try
        {
            var session = Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008.
                        ConnectionString(connectionString).
                        AdoNetBatchSize(50)
                )
                .Mappings(m => m.FluentMappings.Add(typeof(CultureFilter.PonyConditionFilter)))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AgentMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<HotelMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<RoomMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SeasonMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<BookingsMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<CustomerMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PriceMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<GroupRoomsMap>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PermissionsToBooking>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Address>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Owner>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<BookingCancelledBy>())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<RelatedBookings>())
                .ExposeConfiguration(c =>c.SetProperty("current_session_context_class", "thread_static"))
                .BuildSessionFactory();

            session.OpenSession().EnableFilter("PonyConditionFilter").SetParameter("condition", false);
            return session;

        }
        catch (Exception e)
        {
            throw e;
        }
    }

Here is the exception

  [MappingException: Duplicated filter-def named: PonyConditionFilter]
   NHibernate.Cfg.Mappings.AddFilterDefinition(FilterDefinition definition) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Mappings.cs:472
   NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddFilterDefinitions(HbmMapping mappingSchema) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:75
   NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:26
   NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:522

  [MappingException: Could not compile the mapping document: (XmlDocument)]
   NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:342
   NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:530
   NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:499
   NHibernate.Cfg.Configuration.ProcessMappingsQueue() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:1832
   NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:1823
   NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:1816
   NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:632
   NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:483
   NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:455
   FluentNHibernate.PersistenceModel.Configure(Configuration cfg) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\PersistenceModel.cs:283
   FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\MappingConfiguration.cs:88
   FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:249

   [FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.]
Community
  • 1
  • 1
user465154
  • 35
  • 5

1 Answers1

0

You only need to add a single AddFromAssembly line per assembly if all of your mappings reside in the same assembly.

The Fluent NHibernate will scan the entire assembly for your mappings and add them.

try this instead:

var session = Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008.
                        ConnectionString(connectionString).
                        AdoNetBatchSize(50)
                )
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AgentMap>())
                .ExposeConfiguration(c =>c.SetProperty("current_session_context_class", "thread_static"))
                .BuildSessionFactory();