我们的办公室团队正在工作和使用 .NET Framework 4 和 NHibernate 3.3.1 的 ASP.NET 项目
我有一个名为 AllResourcesInfo 的 POCO 域类(它最终映射到数据库中的 Resource 表,该表内部连接到其他一些表,但为了简单起见,假设 AllResourcesInfo 映射到 Resource 表)。
我还有一个名为 Section 的 POCO 域类(最终映射到数据库中的 Section 表)
我还有一个名为 ResourcesInSectionBridge 的 POCO 域类(它最终映射到数据库中名为 ResourcesInSectionBridge 的桥表)
正如命名约定所暗示的,ResourcesInSectionBridge 是 Resource 和 Section 之间的桥接表。
由于 ResourcesInSectionBridge 包含一个复合键,我创建了一个名为 ResourcesInSectionIdentifier 的 POCO 复合键标识符,并且还定义了一个 Equals(object obj) 方法和一个 GetHashCode 方法。
当我使用 NHibernate Session 为 ResourcesInSectionBridge 表保存一个新条目时,我收到一个错误:
异常详细信息:System.Data.SqlClient.SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_ Resources _Resou__22CA2527”冲突。冲突发生在数据库“perls”、表“dbo.Resources”、列“ResourceDatabaseID”中。该语句已终止。
AllResourcesInfo.hbm.xml 的映射如下:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="PivotServer"
namespace="PivotServer.Domain"
auto-import="false">
<class name="AllResourcesInfo" table="Resources">
<id name="ResourceDatabaseID">
<generator class="guid" />
</id>
<property name="ResourceName" />
<property name="DescriptionOfResource" />
<property name="UserId" />
<property name="DepartmentDatabaseID" />
<property name="ResourceStatus" />
<property name="isRemoved" />
<property name="isSubmitted" />
<property name="ResourcePlacement" />
<property name="ResourceType" />
<property name="ParentResourceID" />
<joined-subclass name="Imageitems">
<key column="ResourceDatabaseID"/>
<property column="DirectoryPathToFile" name="location"/>
<property column="ImageIconPath" name="link"/>
<property column="YearOfResource" name="YearOfResource"/>
<property column="ArtistAuthor" name="ArtistAuthor"/>
<property column="UploadDate" name="UploadDate"/>
<property column="ImageitemDatabaseID" name="NativeItemDatabaseID"/>
</joined-subclass>
<joined-subclass name="UriItems">
<key column="ResourceDatabaseID"/>
<property column="uriIconPath" name="location"/>
<property column="UriLink" name="link"/>
<property column="YearOfResource" name="YearOfResource"/>
<property column="ArtistAuthor" name="ArtistAuthor"/>
<property column="UploadDate" name="UploadDate"/>
<property column="UriItemDatabaseID" name="NativeItemDatabaseID"/>
</joined-subclass>
</class>
</hibernate-mapping>
Section.hbm.xml 的映射如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="PivotServer"
namespace="PivotServer.Domain"
auto-import="false">
<class name="Section" table="Section">
<id name="SectionDatabaseID">
<generator class="guid" />
</id>
<many-to-one name="Courses"
column="CourseDatabaseID" class="Courses"
fetch="select"/>
<many-to-one name="instructorUser" column="UserId" class="Users"
fetch="select"/>
<many-to-one name="term"
column="TermDatabaseID" class="Term"
fetch="select"/>
<property column="SectionCode" name="SectionCode" />
<set name="setOfTutorials" inverse="false" lazy="true" fetch="select" >
<key column="TutorialDatabaseID"/>
<one-to-many class="Tutorial"/>
</set>
</class>
</hibernate-mapping>
最后,我们的 ResourcesInSectionBridge.hbm.xml 映射文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="PivotServer"
namespace="PivotServer.Domain"
auto-import="false">
<class name="ResourcesInSectionBridge" table="ResourcesInSectionBridge" lazy="true" >
<composite-id name="ResourcesInSectionIdentifier" class="ResourcesInSectionIdentifier">
<key-property name="ResourceDatabaseID" column="ResourceDatabaseID" type="Guid" />
<key-property name="SectionDatabaseID" column="SectionDatabaseID" type="Guid" />
</composite-id>
<many-to-one name="AllResourcesInfo" class="AllResourcesInfo" cascade="none" insert="false" update="false">
<column name="ResourceDatabaseID"></column>
<column name="ResourceDatabaseID"></column>
</many-to-one>
<many-to-one name="Section" class="Section" cascade="none" insert="false" update="false">
<column name="SectionDatabaseID"></column>
<column name="SectionDatabaseID"></column>
</many-to-one>
</class>
</hibernate-mapping>
最后,我们尝试在 ResourcesInSectionBridge 表中创建条目的 C# 代码如下:
ResourcesInSectionBridge resInSectionBridgeEntity = new ResourcesInSectionBridge();
ResourcesInSectionIdentifier resInSectionIdentifier = new ResourcesInSectionIdentifier();
resInSectionIdentifier.ResourceDatabaseID = aSpecificResource.ResourceDatabaseID;
resInSectionIdentifier.SectionDatabaseID = sectionOfInterest.SectionDatabaseID;
resInSectionBridgeEntity.ResourcesInSectionIdentifier = resInSectionIdentifier;
resInSectionBridgeEntity.Section = sectionOfInterest;
resInSectionBridgeEntity.AllResourcesInfo = aSpecificResource;
using (ISession session = NHibernateHelper.OpenSession())
{
session.Evict(aSpecificResource);
session.Evict(sectionOfInterest);
session.Clear();
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(resInSectionBridgeEntity);
transaction.Commit();
}
} // end of using (ISession session = NHibernateHelper.OpenSession())
当我使用 NHibernate Session 为 ResourcesInSectionBridge 表保存一个新条目时,我收到一个错误:
异常详细信息:System.Data.SqlClient.SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_ Resources _Resou__22CA2527”冲突。冲突发生在数据库“perls”、表“dbo.Resources”、列“ResourceDatabaseID”中。该语句已终止。
你能解释一下为什么会抛出这个错误吗?另外,你能否建议我如何解决这个问题?