NHibernate.MappingException无法确定:Entity的类型

时间:2013-07-03 16:57:54

标签: c# asp.net nhibernate nhibernate-mapping

我有一个asp.net应用程序,并添加了一个包含我的域对象和xml文件的库。

跟踪是:

Message=Could not determine type for: VaLibrary.Core.Domain.VAEmpresa, VaLibrary,    Version=1.0.5.18335, Culture=neutral, PublicKeyToken=null, for columns:    NHibernate.Mapping.Column(IdDev)

源= NHibernate的

我在网上搜索同样的错误,但是,解决方案适用于其他映射选项(流利,城堡等)不适用于xml

我的Code类在这里

using System;
   using System.Text;
   using System.Collections.Generic;
   namespace VaLibrary.Core.Domain {

    public class VAEmpresa {
        private int? _id;
        private string _nombre;
        private string _tipo;
        public VAEmpresa() {
            VAJu_Dev = new List<VAJu>();
            VAJu_Pub = new List<VAJu>();
        }
        public virtual int Id {
            get {
                return (int)this._id;
            }
            set {
                this._id = value;
            }
        }
        public virtual string Nombre {
            get {
                return this._nombre;
            }
            set {
                this._nombre = value;
            }
        }
        public virtual string Tipo {
            get {
                return this._tipo;
            }
            set {
                this._tipo = value;
            }
        }
        public virtual IList<VAJu> VAJu_Dev { get; set; }
        public virtual IList<VAJu> VAJu_Pub { get; set; }
    }
}

并且参考是:

using System;
using System.Text;
using System.Collections.Generic;
namespace VaLibrary.Core.Domain {

    public class VAJu {
        private int? _id;
        private VAEmpresa _idDev;
        private VAEmpresa _idPub;
        private VACon _vACon;
        private string _titulo;
        private int _numerop;
        public VAJu() {
            VApllist = new List<VApllist>();
        }
        public virtual int Id {
            get {
                return (int)this._id;
            }
            set {
                this._id = value;
            }
        }
        public virtual VAEmpresa IdDev {
            get {
                return this._idDev;
            }
            set {
                this._idDev = value;
            }
        }
        public virtual VAEmpresa IdPub {
            get {
                return this._idPub;
            }
            set {
                this._idPub = value;
            }
        }
        public virtual VACon VACon {
            get {
                return this._vACon;
            }
            set {
                this._vACon = value;
            }
        }
        public virtual string Titulo {
            get {
                return this._titulo;
            }
            set {
                this._titulo = value;
            }
        }
        public virtual int Numerop {
            get {
                return this._numerop;
            }
            set {
                this._numerop = value;
            }
        }
        public virtual IList<VApllist> VApllist { get; set; }
    }
}

映射文件是:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="VaLibrary" namespace="VaLibrary.Core.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="VAEmpresa" table="VA-Empresa" lazy="true" >
    <id name="Id" column="Id">
      <generator class="identity" />
    </id>
    <property name="Nombre">
      <column name="nombre" sql-type="varchar" not-null="true" />
    </property>
    <property name="Tipo">
      <column name="tipo" sql-type="varchar" not-null="false" />
    </property>
    <bag name="VAJu_Dev" inverse="true" cascade="none">
      <key column="IdDev" />
      <one-to-many class="VaLibrary.Core.Domain.VAJu, VaLibrary" />
    </bag>
    <bag name="VAJu_Pub" inverse="true" cascade="none">
      <key column="IdPub" />
      <one-to-many class="VaLibrary.Core.Domain.VAJu, VaLibrary" />
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="VaLibrary" namespace="VaLibrary.Core.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="VAJu" table="VA-Ju" lazy="true" >
    <id name="Id" column="Id">
      <generator class="identity" />
    </id>
    <property name="Titulo">
      <column name="Titulo" sql-type="varchar" not-null="true" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="IdDev">
      <column name="IdDev" sql-type="int" not-null="true" />
    </many-to-one>
    <property name="IdDev">
      <column name="IdDev" sql-type="int" not-null="true" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="IdPub">
      <column name="IdPub" sql-type="int" not-null="true" />
    </many-to-one>
    <property name="IdPub">
      <column name="IdPub" sql-type="int" not-null="true" />
    </property>  
    <property name="Numerop">
      <column name="numero-p" sql-type="int" not-null="true" />
    </property>
    <bag name="VApllist" inverse="true" cascade="none">
      <key column="IdJu" />
      <one-to-many class="VApllist" />
    </bag>
  </class>
</hibernate-mapping>

DB中的关系是: Va-Ju.IdDev FK到VA-Empresa.Id PK Va-JU.IdPub FK到VA-Empresa.Id PK

我试着改变  但是是一样的。

我能做到吗?

非常感谢您的回答

[编辑]

我使用Nhibernate 3.2

2 个答案:

答案 0 :(得分:1)

映射缺少正确的关系 (对象到对象)映射。如何解释这个问题的最好方法是:

改变这个:

private VAEmpresa _idDev;

public virtual VAEmpresa IdDev 
{
  get { return this._idDev; }
  set { this._idDev = value; }
}

进入这个:

private VAEmpresa _dev;

public virtual VAEmpresa Dev  // Reference Object
{
  get { return this._idDev; }
  set 
  { 
      this._idDev = value; 
      if(_idDev != null)      // assure that with reference change
      {                       // the referenceId is changed as well
          IdDev = _idDev.Id
      }
  }
}
public virtual int IdDev { get; set; } // Refernece Id

现在我们有Dev引用的两种表示形式(类型为VAEmpresa且具有int id)。我们可以按照工作方式进行映射。所以

改变这个:

<many-to-one insert="false" update="false" lazy="false" name="IdDev">
  <column name="IdDev" sql-type="int" not-null="true" />
</many-to-one>
<property name="IdDev">
  <column name="IdDev" sql-type="int" not-null="true" />
</property>

进入那个:

<many-to-one name="Dev" class="VAEmpresa" column="IdDev"
         insert="false" update="false" lazy="false" />
<property name="IdDev" not-null="true" />

从那时起,NHibernate将知道在DB中搜索Dev属性的位置。它将是列IdDev,实例化的类是VAEmpresa

如果代码只需要 referenceId IdDev ......我们也有。

SMALL注意:我的经验是使用不同的 readonly 设置。可编辑是参考,readonly是参考。这会强制代码与现有的VAEmpresa对象一起使用,而IdDev可以是任何整数。处理很复杂,但后来会带来成果

答案 1 :(得分:0)

ID可能需要一种类型。你有:

<id name="Id" column="Id">
  <generator class="identity" />
</id>

尝试按如下方式指定类型:

<id name="Id" >
  <column name="Id" sql-type="int" not-null="true" unique="true" />
  <generator class="identity" />
</id>

另一个问题是您忘记将列/域的名称放在bag属性中。您需要输入实际的列名而不是属性名。让我告诉你VAJu:

 <bag name="VApllist" inverse="true" cascade="none">
  <key column="Id"  />
  <one-to-many class="VaLibrary.Core.Domain.VApllist" column="Id"  />
 </bag>