【问题标题】:NHibernate doesn't see table, MappingExceptionNHibernate 看不到表,MappingException
【发布时间】:2013-05-06 20:26:00
【问题描述】:

我以为我对 NHibernate 了如指掌,但我一定是在做一些愚蠢的事情。我有一个名为 Category 的表/类。当我从我的 GetAll 方法中提取数据时,没有任何返回,但也没有错误。

班级是:

namespace Model
{
    [Serializable]
    public partial class Category
    {
        public virtual int Id { get; set; }
        public virtual DateTime CreatedOn { get; set; }
        public virtual DateTime UpdatedOn { get; set; }

        public virtual string Name { get; set; }

        public override bool Equals(object oneObject)
        {
            return oneObject is Category && (this.GetHashCode() == ((Category)oneObject).GetHashCode());
        }

        public override int GetHashCode()
        {
            return Id.ToString().GetHashCode();
        }

    }
}

映射文件:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="Model" assembly="Model" xmlns="urn:nhibernate-mapping-2.2">
    <class name="Category" lazy="true" table="`categories`"><!--test only!!-->
        <id name="Id" access="property" column="`category_id`">
            <generator class="native" />
        </id>
    <property name="Name" column="`name`" length="50" />
    </class>
</hibernate-mapping>

如果我在另一个表中添加多对一引用,则会出错:An association from the table manufacturer_categories refers to an unmapped class: Model.Category

在我看来,NHibernate 显然无法识别我的映射文件。我错过了什么愚蠢的事情?

【问题讨论】:

  • Didja 检查明显的东西,比如将 .hbm 部分添加到 xml 文件中,或者确保将其设置为嵌入内容?
  • @Cosmo_D -- 是的,我检查了 .hbm.xml。 “嵌入式内容”是关于什么的?
  • @Rippo - 就是这样!我什至没有考虑检查资源。调试这样的东西有点困难。如果您想发布答案,我很乐意接受。谢谢。
  • 答案已添加,很高兴就这么简单!

标签: c# nhibernate


【解决方案1】:

使用类的全限定名,可以解决问题

<class name="Modle.Category" lazy="true" table="`categories`">

还要确保在配置 Nhibernate 时添加了包含类别映射文件的程序集

Configuration cfg = new Configuration();
cfg.Configure();

// Add class mappings to configuration object
Assembly mappingAssembly = AssemblyContatingTheCategoryMappingXMLFile;
cfg.AddAssembly(mappingAssembly);

另一个提示是在属性选项卡上将 xml 文件设置为 Embedded Resource

【讨论】:

  • 我添加了一些可能会派上用场的建议。
【解决方案2】:

您是否检查过您的 XML 文件被标记为嵌入式资源?

【讨论】:

    猜你喜欢
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多