2011-11-11 125 views
0

我有以下型號拋出:映射例外由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 

有人能指出我在哪裏,我聽錯了?

回答

0

嘗試使用WeatherForecast映射上多對一關聯的全限定類名稱。

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

非常感謝。有用。 –