简体   繁体   English

NHibernate配置,外键

[英]NHibernate configuration, foreign keys

I must admit I'm still pretty new to NHibernate, so please go easy on me. 我必须承认我还是NHibernate的新手,所以请放轻松。

I have 2 POCOs, a Log and a UserProfile . 我有2个POCO,一个Log和一个UserProfile If it is not inherently clear, the log entry can reference a user profile, and a user profile can have many log entries associated with it. 如果本质上不清楚,则日志条目可以引用用户概要文件,并且用户概要文件可以具有与其关联的许多日志条目。 The mapping files look like this: 映射文件如下所示:

<?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>

When I'm trying to build my schema against a PostgreSQL database, I receive the following error: 当我尝试针对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

The SQL it's trying to run at the time of death is: 它在死亡时试图运行的SQL是:

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

...which is clearly not what I was expecting it to do, but I don't know where to fix it. ...这显然不是我期望的那样,但是我不知道在哪里修复。 Tell me what I'm doing wrong first, and get some quick rep. 告诉我我做错了什么,然后快速获取代表。 If you can explain it so I can understand where I went wrong, I'd be most appreciative. 如果您能解释一下,这样我就能理解我哪里出了问题,我将非常感激。

if you do not want the foreign key then get rid of the following line from your mapping: 如果您不希望使用外键,请从映射中删除以下行:

<key column="LogId" />

In your question you have not specified what you expect it do. 在您的问题中,您没有指定期望的功能。

The answer was simple; 答案很简单; I wish the error reflected what was truly wrong. 我希望错误反映出真正的错误。

I was trying to set up a bidirectional relationship by defining it in the mappings for both; 我试图通过在两者的映射中定义双向关系来建立双向关系。 this was both unnecessary and incorrect. 这既不必要又不正确。 I removed the 我删除了

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

element from my UserProfile configuration, and the error went away. 我的UserProfile配置中的元素,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM