NHibernate没有看到表,MappingException

时间:2013-05-06 20:26:00

标签: c# nhibernate

我以为我知道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没有识别我的映射文件。我错过了什么愚蠢的事情?

2 个答案:

答案 0 :(得分:2)

使用班级的完全限定名称,可以解决问题

<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文件设置为properety选项卡上的Embedded Resource

答案 1 :(得分:1)

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

enter image description here