我必须承认我对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 :(得分:0)
如果您不想要外键,那么请从映射中删除以下行:
<key column="LogId" />
在你的问题中,你没有说明你的期望。
答案 1 :(得分:0)
答案很简单;我希望这个错误反映出真正的错误。
我试图通过在两者的映射中定义双向关系来建立双向关系;这既不必要又不正确。我删除了
<bag name="ActivityLogs" cascade="none" table="Logs" >
<key column="LogId" />
<one-to-many class="Log"/>
</bag>
我的UserProfile配置中的元素,错误就消失了。