2012-04-19 39 views
1

我必須承認我對NHibernate還是很新的,所以請在我身上輕鬆一點。NHibernate配置,外鍵

我有2個POCO,一個Log和一個UserProfile。如果它本質上不明確,則日誌條目可以引用用戶配置文件,並且用戶配置文件可以具有與其關聯的許多日誌條目。映射文件是這樣的:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="C3.DataModel.Generated" 
        namespace="C3.DataModel"> 
    <class name="Log" table="Logs"> 
     <id name="LogId"> 
      <generator class="guid" /> 
     </id> 
     <property name="TimeStamp" column="TimeStamp" index="ixLogTimeStamp" not-null="false" /> 
     <property name="Thread" length="255" column="Thread" not-null="false" /> 
     <property name="Severity" column="Severity" /> 
     <property name="Source" length="255" not-null="false" column="Source" /> 
     <property name="Message" length="4000" not-null="false" column="Message" /> 
     <property name="Exception" length="4000" column="Exception" not-null="true" /> 
     <many-to-one name="UserProfile" not-null="false" class="UserProfile" foreign-key="UserID" column="UserID" /> 
    </class> 
</hibernate-mapping> 

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="C3.DataModel.Generated" 
        namespace="C3.DataModel"> 
    <class name="UserProfile" table="UserProfiles"> 
     <id name="UserID"> 
      <generator class="assigned" /> 
     </id> 
     <property name="GivenName" length="40" column="GivenName" /> 
     <property name="MiddleName" length="40" column="MiddleName" not-null="false" /> 
     <property name="FamilyName" length="40" column="FamilyName" /> 
     <property name="UserName" length="250" index="ixUserName" unique="true" /> 
     <property name="NickName" length="40" column="NickName" /> 
     <property name="Address1" length="50" column="Address1" /> 
     <property name="Address2" length="50" column="Address2" /> 
     <property name="City" length="50" column="City" /> 
     <property name="State" length="2" column="State" /> 
     <property name="ZipCode" length="10" column="ZipCode" /> 
     <property name="Bio" length="2000" column="Bio" not-null="false" /> 
     <property name="BirthDate" not-null="false" column="BirthDate" /> 
     <property name="LinkToAvatar" length="250" column="LinkToAvatar" not-null="false" /> 
     <property name="LinkToWebpage" length="250" column="LinkToWebPage" not-null="false" /> 
     <bag name="ActivityLogs" cascade="none" table="Logs" > 
      <key column="LogId" /> 
      <one-to-many class="Log"/> 
     </bag> 
    </class> 
</hibernate-mapping> 

當我試圖建立我的架構對PostgreSQL數據庫,我收到以下錯誤:

NHibernate.HibernateException: ERROR: 42804: foreign key constraint "fkf9c1212b275a3569" cannot be implemented ---> Npgsql.NpgsqlException: ERROR: 42804: foreign key constraint "fkf9c1212b275a3569" cannot be implemented 
    at Npgsql.NpgsqlState.<ProcessBackendResponses_Ver_3>d__a.MoveNext() 
    at Npgsql.ForwardsOnlyDataReader.GetNextResponseObject() 
    at Npgsql.ForwardsOnlyDataReader.GetNextRowDescription() 
    at Npgsql.ForwardsOnlyDataReader.NextResult() 
    at Npgsql.ForwardsOnlyDataReader..ctor(IEnumerable`1 dataEnumeration, CommandBehavior behavior, NpgsqlCommand command, NotificationThreadBlock threadBlock, Boolean synchOnReadError) 
    at Npgsql.NpgsqlCommand.GetReader(CommandBehavior cb) 
    at Npgsql.NpgsqlCommand.ExecuteNonQuery() 
    at NHibernate.Tool.hbm2ddl.SchemaExport.ExecuteSql(IDbCommand cmd, String sql) 
    at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean throwOnError, TextWriter exportOutput, IDbCommand statement, String sql) 
    at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop, IDbConnection connection, TextWriter exportOutput) 
    at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop) 
    --- End of inner exception stack trace --- 
    at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop) 
    at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop) 
    at NHibernate.Tool.hbm2ddl.SchemaExport.Create(Boolean script, Boolean export) 
    at C3.DataModel.Repositories.NHibernateHelper.CreateSchema() in C:\projects\C3\C3.DataModel.Generated\Repositories\NHibernateHelper.cs:line 41 

的SQL它試圖在當時運行的死亡是:

alter table Logs add constraint FKF9C1212B275A3569 foreign key (LogId) references UserProfiles 

......這顯然不是我期待它做的,但我不知道在哪裏解決它。告訴我我第一次做錯了什麼,並得到一些快速的代表。如果你能解釋它,這樣我就能明白我出錯的地方,我會非常感激。

+0

您的日誌映射包含,並且'column'和'foreign-keys'都被命名爲UserId。它是否正確? – Rippo 2012-04-19 08:10:42

+0

我嘗試了幾種不同的方式(沒有注意到外鍵,而沒有注意到列......),它似乎沒有什麼區別。 – 2012-04-19 11:10:38

回答

0

答案很簡單;我希望這個錯誤反映了真正的錯誤。

我試圖通過在兩者的映射中定義它來建立雙向關係;這是不必要的和不正確的。我從我的UserProfile配置中刪除了

<bag name="ActivityLogs" cascade="none" table="Logs" > 
     <key column="LogId" /> 
     <one-to-many class="Log"/> 
    </bag> 

元素,並且錯誤消失了。

0

,如果你不想讓外鍵然後得到從你的映射去掉下面一行:

<key column="LogId" /> 

在你的問題,你沒有指定你希望它做什麼。

+0

我想要用戶配置文件和日誌條目之間的雙向關係。 – 2012-04-20 01:54:05