NHibernate“无法确定X的类型”错误

时间:2011-02-18 17:27:55

标签: c# nhibernate fluent-nhibernate

在项目中升级NHibernate和FluentNHibernate DLL之后,我现在得到了一个"无法确定类型:MyApp.Domain.Entities.AppCategory"初始化SessionFactory时抛出的异常。我的代码中唯一的变化就是调整ForeignKeyConvention的实现以覆盖GetKeyName ( Member member, Type type )抽象方法,而不是GetKeyName ( PropertyInfo property, Type type )

对于FluentNHibernate,升级后的DLL从1.0.0.593到1.1.0.685,对于NHibernate,升级后的DLL从2.1.0.4000到2.1.2.4000。寻找解决方案的部分困难在于我们使用的NHibernate版本的老年,但至少目前还无法改变。

我在下面发布了完整的例外以及所有相关的代码和配置。我为长度道歉,但我不知道问题可能在哪里。

完全例外

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

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

  ----> NHibernate.MappingException : Could not compile the mapping document: (XmlDocument)
  ----> NHibernate.MappingException : Could not determine type for: MyApp.Domain.Entities.AppCategory, MyApp.Domain, Version=1.0.0.76, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(AppCategory)
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs: line 98
at MyFramework.App.DataAccess.NHibernate.Databases.BaseDatabase.CreateSessionFactory() in C:\WIP\VSProjects\MyFramework\src\App\DataAccess\NHibernate\Databases\BaseDatabase.cs: line 115
at MyFramework.App.DataAccess.NHibernate.Databases.BaseDatabase.Init() in C:\WIP\VSProjects\MyFramework\src\App\DataAccess\NHibernate\Databases\BaseDatabase.cs: line 100
at MyApp.DataAccess.SmartStudioUserUnitOfWork.<.ctor>b__0() in SmartStudioUserUnitOfWork.cs: line 28
at MyFramework.App.DataAccess.NHibernate.UnitOfWork`1.Create(FlushMode flushMode) in C:\WIP\VSProjects\MyFramework\src\App\DataAccess\NHibernate\UnitOfWork.cs: line 72
at MyFramework.App.DataAccess.NHibernate.UnitOfWork`1..ctor(Func`1 getBaseDatabase) in C:\WIP\VSProjects\MyFramework\src\App\DataAccess\NHibernate\UnitOfWork.cs: line 37
at MyApp.DataAccess.SmartStudioUserUnitOfWork..ctor() in SmartStudioUserUnitOfWork.cs: line 17
at MyApp.DataAccess.Test.SmartStudioUserDaoTest.create_dao() in SmartStudioUserDaoTest.cs: line 20
--FluentConfigurationException
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs: line 119
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs: line 93
--MappingException
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc)
at FluentNHibernate.PersistenceModel.Configure(Configuration cfg) in d:\Builds\FluentNH\src\FluentNHibernate\PersistenceModel.cs: line 262
at FluentNHibernate.Automapping.AutoPersistenceModel.Configure(Configuration configuration) in d:\Builds\FluentNH\src\FluentNHibernate\Automapping\AutoPersistenceModel.cs: line 170
at FluentNHibernate.Cfg.AutoMappingsContainer.Apply(Configuration cfg) in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\AutoMappingsContainer.cs: line 84
at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\MappingConfiguration.cs: line 56
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs: line 110
--MappingException
at NHibernate.Mapping.SimpleValue.get_Type()
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindProperty(XmlNode node, Property property, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.CreateProperty(IValue value, String propertyName, String className, XmlNode subnode, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindJoin(XmlNode node, Join join, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.PropertiesFromXML(XmlNode node, PersistentClass model, IDictionary`2 inheritedMetas, UniqueKey uniqueKey, Boolean mutable, Boolean nullable, Boolean naturalId)
at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode parentNode, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) 

流畅配置

sessionFactory = fluentConfiguration
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<Application>().Conventions
            .Add<CustomForeignKeyConvention>()))
    .ExposeConfiguration(new SchemaExport(config).Create(true, false))
    .BuildSessionFactory();

public class CustomForeignKeyConvention : ForeignKeyConvention
{
    protected override string GetKeyName ( Member member, Type type )
    {
        if (member == null)
        {
            return type.Name + "Id";
        }

        return member.Name + "Id";
    }
}

域类

public class Application
{
    public virtual int Id { get; set; }
    public virtual string FriendlyName { get; set; }
    public virtual AppCategory AppCategory { get; set; }
}

public class AppCategory
{
    public virtual int Id { get; private set; }
    public virtual string CategoryName { get; set; }
}

数据库表定义

CREATE TABLE [dbo].[Application](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FriendlyName] [varchar](255) NOT NULL,
    [AppCategoryId] [int] NOT NULL,
    CONSTRAINT [PK_Application] PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO
ALTER TABLE [dbo].[Application] WITH CHECK
        ADD CONSTRAINT [FK_Application_AppCategory] FOREIGN KEY([AppCategoryId])
        REFERENCES [dbo].[AppCategory] ([Id])
GO
ALTER TABLE [dbo].[Application] CHECK CONSTRAINT [FK_Application_AppCategory]
GO

CREATE TABLE [dbo].[AppCategory](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CategoryName] [nvarchar](50) NOT NULL,
    CONSTRAINT [PK_AppCategory] PRIMARY KEY CLUSTERED ([Id] ASC)
)

流利的NHibernate制图

public class ApplicationMappingOverride : IAutoMappingOverride<Application>
{
    public void Override(AutoMapping<Application> mapping)
    {
        mapping.Table("Application");
        mapping.Id(x => x.Id);
        mapping.Map(x => x.FriendlyName);
        mapping.Join("AppCategory", x => x.Map(y => y.AppCategory));
    }
}

映射XML(从Fluent NHibernate日志消息中提取):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">

    <class xmlns="urn:nhibernate-mapping-2.2" mutable="true"
        name="MyApp.Domain.Entities.Application, MyApp.Domain, Version=1.0.0.76, Culture=neutral, PublicKeyToken=null" table="Application">

        <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="Id" />
            <generator class="identity" />
        </id>

        <property name="FriendlyName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="FriendlyName" />
        </property>

        <join table="AppCategory">
            <key>
                <column name="ApplicationId" />
            </key>
            <property name="AppCategory" type="MyApp.Domain.Entities.AppCategory, MyApp.Domain, Version=1.0.0.76, Culture=neutral, PublicKeyToken=null">
                <column name="AppCategory" />
            </property>
        </join>

    </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:38)

也许为时已晚,但您仍会对我找到的解决方案感兴趣。当我试图覆盖我的一个实体的映射时,我遇到了同样的问题。

此错误的典型原因是当您尝试为复杂类型调用Map()时。在这种情况下,Fluent NHibernate试图找出要映射到相应数据库类型的此实体的类型(例如intintegerboolbit等等,并且只要AppCategory类型没有这种对应就失败了。

因此,如果您要更改以下行

,则可能会修复您的错误
 mapping.Join("AppCategory", x => x.Map(y => y.AppCategory));

到这个

 mapping.Join("AppCategory", x => x.References(y => y.AppCategory));