gpt4 book ai didi

c# - NHibernate 配置,外键

转载 作者:行者123 更新时间:2023-11-29 14:01:56 25 4
gpt4 key购买 nike

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

...这显然不是我所期望的,但我不知道在哪里修复它。首先告诉我我做错了什么,然后快速获得一些代表。如果您能解释一下,以便我了解哪里出了问题,我将不胜感激。

最佳答案

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

<key column="LogId" />

在你的问题中你没有指定你期望它做什么。

关于c# - NHibernate 配置,外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10220973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com