2009-12-16 19 views
2

在Fluent NHibernate Mapping中一起使用Join和Component拋出「無法找到屬性異常的getter」。這是我的C#代碼在Fluent NHibernate Mapping中一起使用Join和Component拋出「無法找到屬性異常的getter」

using FluentNHibernate.Mapping; 

namespace FnhTest { 
    public class CustomerMap : ClassMap<Customer> { 
     public CustomerMap() { 
      Id(x => x.Id).GeneratedBy.Identity(); 
      Map(x => x.Name); 

      Join("BillingInfo", m => 
           { 
            m.KeyColumn("CustomerId"); 
            m.Component(x => x.BillingInfo, c => 
                    { 
                     c.Map(y => y.AccountNumber); 
                     c.Map(y => y.Address); 
                    }); 
           }); 
     } 
    } 

    public class BillingInfo { 
     public virtual string AccountNumber { get; set; } 
     public virtual string Address { get; set; } 
    } 

    public class Customer { 
     public virtual int Id { get; set; } 
     public virtual string Name { get; set; } 

     public virtual BillingInfo BillingInfo { get; set; } 
    } 
} 

,這是我的數據庫結構=>

Customers: 
    Id (int) 
    Name (varchar 50) 
BillingInfo: 
    Id (int) 
    AccountNumber (varchar 50) 
    Address (varchar 50) 
    CustomerId (int) (Foriegn Key to the Customers Id) 

功能NHibernate生成這種設置正確的映射,但由於某些原因,它拋出了一個錯誤。下面給出的映射和

Mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="FnhTest.Customer, FnhTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Customer`"> 
    <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="Id" /> 
     <generator class="identity" /> 
    </id> 
    <property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="Name" /> 
    </property> 
    <join table="BillingInfo"> 
     <key> 
     <column name="CustomerId" /> 
     </key> 
     <component name="BillingInfo" insert="true" update="true" optimistic-lock="true"> 
     <property name="AccountNumber" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
      <column name="AccountNumber" /> 
     </property> 
     <property name="Address" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
      <column name="Address" /> 
     </property> 
     </component> 
    </join> 
    </class> 
</hibernate-mapping> 

Error:

TestCase 'M:FnhTest.Program.Main(System.String[])' 
failed: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. 

    * Database was not configured through Database method. 

    FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. 

     * Database was not configured through Database method. 
    ---> NHibernate.PropertyNotFoundException: Could not find a getter for property 'AccountNumber' in class 'FnhTest.Customer' 
    at NHibernate.Properties.BasicPropertyAccessor.GetGetter(Type type, String propertyName) 
    at NHibernate.Tuple.Component.PocoComponentTuplizer.BuildGetter(Component component, Property prop) 
    at NHibernate.Tuple.Component.AbstractComponentTuplizer..ctor(Component component) 
    at NHibernate.Tuple.Component.PocoComponentTuplizer..ctor(Component component) 
    at NHibernate.Tuple.Component.ComponentEntityModeToTuplizerMapping..ctor(Component component) 
    at NHibernate.Tuple.Component.ComponentMetamodel..ctor(Component component) 
    at NHibernate.Mapping.Component.BuildType() 
    at NHibernate.Mapping.Component.get_Type() 
    at NHibernate.Mapping.SimpleValue.IsValid(IMapping mapping) 
    at NHibernate.Mapping.PersistentClass.Validate(IMapping mapping) 
    at NHibernate.Mapping.RootClass.Validate(IMapping mapping) 
    at NHibernate.Cfg.Configuration.Validate() 
    at NHibernate.Cfg.Configuration.BuildSessionFactory() 
    d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs(93,0): at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() 
     --- End of inner exception stack trace --- 
    d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs(100,0): at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() 
    D:\repositories\core\playground\minhajuddin\FnhTest\FnhTest\Program.cs(8,0): at FnhTest.Program.Main(String[] args) 

     * Database was not configured through Database method. 


0 passed, 1 failed, 0 skipped, took 6.46 seconds (Ad hoc). 

我已經搜索所有網站上,但未能找到任何有用的錯誤,任何形式的幫助將是非常感謝:)

編輯: 那麼,我沒有找到一種方式來做到這一點在流利NHibernate,我是我們無論Torkel發佈什麼答案。但是,那不是我的意圖。無論如何。

回答

2

BillingInfo在我看來不像組件,而是實體。

如果將BillingInfo映射爲實體,則可以將其作爲關聯映射到Customer。

<many-to-one name="BillingInfo" property-ref="CustomerId" cascade="none"/> 

,你不想加入上的BillingInfo標識但在客戶的財產裁判importand,不過這需要你的客戶編號屬性添加到您的BillingInfo類。

+0

是的,這是可以做到的,但是當我有一個客戶時,我總是需要一個BillingInfo對象,我知道這可以通過一對一的映射完成,但是我更喜歡這種方式,因爲它會自動執行在這種情況下,'外部連接' – 2009-12-16 12:48:10

+0

除非您正確設計,否則它不會起作用。托克爾的建議是健全的。 – 2009-12-16 13:31:38

+0

我同意托克爾的說法,但我只是想知道爲什麼它不可能在流利NHibernate,當可能在NHibernate中做同樣的事情。 – 2009-12-17 11:09:39

0

請原諒我,如果這是一個愚蠢的觀察,但是你不需要在BillingInfo中的Id成員?

+0

我在BillingInfo類中有一個Id字段,但它有點多餘,因爲它沒有被NHibernate填充 – 2009-12-19 07:22:38

相關問題