2012-04-19 68 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