0

我是 fluent hibernate 的新手,在尝试创建过滤器时遇到异常。

我发现这个语法用 Fluent Nhibernate 定义 NHibernate 过滤器?

它解释了如何创建一个流畅的过滤器,直到我尝试运行我的应用程序。我得到一个例外(见下文)。我的代码与上面发布的链接相同。这是导致异常的代码。如果我只为映射添加 1 个程序集,例如 HotelMap,问题就消失了,当我添加 2 个或更多时,我得到了异常。

任何想法将不胜感激。

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;
        }
    }

这是例外

  [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.]
4

1 回答 1

0

如果所有映射都位于同一个程序集中,则只需为每个程序集添加一个 AddFromAssembly 行。

Fluent NHibernate 将扫描整个程序集以查找您的映射并添加它们。

试试这个:

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();
于 2013-10-08T20:39:27.990 回答