映射NHibernate引发的异常

时间:2011-11-11 18:10:03

标签: asp.net-mvc database hibernate nhibernate

我有以下型号:

WeatherForecast:

namespace TravelAssistant.Model.HelperModels.Weather
public class WeatherForecast:EntityBase<int>
{
    public IList<DayForecast> ForecastDays { get; set; }

    public LocationBase ForecastLocation  { get; set; }

    public void Init(Location.LocationBase location)
    {
        this.ForecastLocation = location;
    }

    protected override void Validate()
    {
        throw new NotImplementedException();
    } 
}

LocationBase:

 namespace TravelAssistant.Model.HelperModels.Location
    public class LocationBase:EntityBase<int>
    {  

        public string WorldRegion { get; set; }

        public string City { get; set; }

        public string Country { get; set; }

        public string Zipcode { get; set; }

        public string StreetNameNumber { get; set; }
   }

其中EntityBase定义类型为T的id,以及以下.hbm文件

WeatherForecast.hbm.xml:

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
        namespace="TravelAssistant.Model.HelperModels.Weather"
                assembly="TravelAssistant.Model">

    <class name="WeatherForecast" table="WeatherForecasts" lazy="false" >
        <id  name="Id" column="WeatherForecastID"  
                           type="int" unsaved-value="0">
            <generator class="native" />
        </id>

        <many-to-one name="ForecastLocation"
                   cascade="all" 
                   class="LocationBase" 
                   column="LocationID" 
                   not-null="false" />

        <bag name="ForecastDays" lazy="false" >
            <key column="WeatherForecastID"/>
            <one-to-many class="DayForecast"></one-to-many>
        </bag>
    </class>
</hibernate-mapping>

LocationBase.hbm.xml

           

    <id  name="Id" column="LocationID" type="int" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="City">
      <column name="City" 
              sql-type="varchar(150)" not-null="true" />
    </property>
    <property name="Country">
      <column name="Country" 
              sql-type="varchar(50)" not-null="true" />
    </property>
    <property name="Zipcode">
      <column name="Zipcode" 
              sql-type="varchar(50)" not-null="false" />
    </property>
    <property name="StreetNameNumber">
      <column name="StreetNameNumber" 
              sql-type="varchar(150)" not-null="false" />
    </property>
    <property name="WorldRegion">
      <column name="WorldRegion" 
              sql-type="varchar(100)" not-null="false" />
    </property>
  </class>
</hibernate-mapping>

数据库模型是:

WeatherForecasts : WeatherForecastID, LocationID
Locations:LocationID,City,Country,Zipcode, StreetNameNumber,WorldRegion.

当我运行程序时,我得到:

表WeatherForecasts中的关联引用了未映射的类:

TravelAssistant.Model.HelperModels.Weather.LocationBase

有人能指出我弄错了吗?

1 个答案:

答案 0 :(得分:0)

尝试在WeatherForecast映射上使用完全限定的类名作为多对一关联。

<many-to-one     
    name="ForecastLocation"
    cascade="all"
    class="TravelAssistant.Model.HelperModels.Location.LocationBase, TravelAssistant.Model"
    column="LocationID"
    not-null="false"   />