从多个程序集加载时,使用Fluent NHibernate复制类映射

时间:2011-06-30 18:10:08

标签: nhibernate nhibernate-mapping fluent

我在我的网络应用程序中使用Fluent和NHibernate V2.4。

由于我的设计,我在多个程序集中都有映射文件。

然而,当我尝试将第二个程序集添加到映射中时(请参阅下面的代码),我得到一个错误,说该类(此时第二个程序集中唯一的一个)已经被映射(不用说我只是每个类有一个映射类。)

你能看到我出错的地方吗?

错误:

    Duplicate class/entity mapping

     NHibernate.Cfg.Mappings.AddClass(PersistentClass persistentClass) +181
   NHibernate.Cfg.XmlHbmBinding.JoinedSubclassBinder.HandleJoinedSubclass(PersistentClass model, XmlNode subnode, IDictionary`2 inheritedMetas) +1834
   NHibernate.Cfg.XmlHbmBinding.ClassBinder.PropertiesFromXML(XmlNode node, PersistentClass model, IDictionary`2 inheritedMetas, UniqueKey uniqueKey, Boolean mutable, Boolean nullable, Boolean naturalId) +1911
   NHibernate.Cfg.XmlHbmBinding.ClassBinder.PropertiesFromXML(XmlNode node, PersistentClass model, IDictionary`2 inheritedMetas) +64
   NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas) +1214
   NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode parentNode, IDictionary`2 inheritedMetas) +270
   NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node) +162
   NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) +262

[MappingException: Could not compile the mapping document: (XmlDocument)]
   NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) +136
   NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) +387
   NHibernate.Cfg.Configuration.ProcessMappingsQueue() +74
   NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) +64
   NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) +94
   NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) +158
   NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name) +323
   NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc) +72
   FluentNHibernate.PersistenceModel.Configure(Configuration cfg) +751
   FluentNHibernate.Cfg.FluentMappingsContainer.Apply(Configuration cfg) +472
   FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) +332
   FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() +108

[FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

]
   FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() +325
   Liath.Surveys.Dao.NHibernate.BurrowFluentConfigurator.Config(IPersistenceUnitCfg puCfg, Configuration nhCfg) in C:\Subversion\Surveys\Source\Liath.Surveys.Dao.NHibernate\BurrowFluentConfigurator.cs:49
   NHibernate.Burrow.Impl.PersistenceUnit..ctor(IPersistenceUnitCfg cfg, IConfigurator configurator) +148
   NHibernate.Burrow.Impl.PersistenceUnitRepo.Initialize(IBurrowConfig configuration) +212
   NHibernate.Burrow.Impl.FrameworkEnvironment.Start() +185
   NHibernate.Burrow.Impl.FrameworkEnvironment.ReConfig(IConfigurator configurator) +159
   NHibernate.Burrow.Impl.FrameworkEnvironment..ctor() +55
   NHibernate.Burrow.Impl.FrameworkEnvironment..cctor() +53

[TypeInitializationException: The type initializer for 'NHibernate.Burrow.Impl.FrameworkEnvironment' threw an exception.]
   NHibernate.Burrow.Impl.FrameworkEnvironment.get_Instance() +60
   NHibernate.Burrow.Impl.WorkSpace.get_Current() +50
   NHibernate.Burrow.BurrowFramework.get_WorkSpaceIsReady() +59
   NHibernate.Burrow.BurrowFramework.CloseWorkSpace() +62
   NHibernate.Burrow.WebUtil.WebUtilHTTPModule.CloseContext(Object sender, EventArgs e) +103
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

以下是配置器的代码:

 public void Config(IPersistenceUnitCfg puCfg, Configuration nhCfg)
    {
        try
        {
            string[] assemblies =
            { 
                "AssemblyA",
                "AssemblyB"
            }; // TODO: push down into config

            FluentConfiguration fluentCfg = Fluently.Configure(nhCfg);
            fluentCfg = fluentCfg.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005
                .ConnectionString(@"Server=localhost\SQLEXPRESS;initial catalog=MyDatabase;Integrated Security=SSPI"));

            foreach (string assemblyName in assemblies)
            {
                fluentCfg = fluentCfg.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load(assemblyName)));
            }
            fluentCfg.BuildConfiguration();
        }
        catch (Exception ex)
        {
            s_log.Error("There was an error configuring NHibernate", ex);
            throw;
        }
    }

0 个答案:

没有答案