NHibernate配置,外键

本文关键字:外键 配置 NHibernate | 更新日期: 2023-09-27 18:31:47

我必须承认我对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

。这显然不是我期望它做的事情,但我不知道在哪里修复它。 先告诉我我做错了什么,然后快速回复。 如果你能解释一下,这样我就能理解我哪里出错了,我将不胜感激。

NHibernate配置,外键

如果您不想要外键,请从映射中删除以下行:

<key column="LogId" />

在您的问题中,您没有指定您希望它做什么。

答案很简单;我希望这个错误能反映真正的错误所在。

我试图通过在两者的映射中定义它来建立双向关系;这既不必要又不正确。 我删除了

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

元素,错误消失了。