Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:44:46+00:00 2026-06-18T08:44:46+00:00

Our office team is working and ASP.NET project that uses .NET Framework 4 and

  • 0

Our office team is working and ASP.NET project that uses .NET Framework 4 and NHibernate 3.3.1

I have a POCO domain class called AllResourcesInfo ( which ultimately maps to a Resource table in the database that is inner joined to some other tables, but just to keep things simple, let’s just say that the AllResourcesInfo maps to the Resource table ).

I also have a POCO domain class called Section ( which ultimately maps to a Section table in the database )

I also have a POCO domain class called ResourcesInSectionBridge (which ultimately maps to a bridge table in the database called ResourcesInSectionBridge )

As the naming convention suggests, ResourcesInSectionBridge is a bridge table between the Resource and Section.

Since the ResourcesInSectionBridge contains a composite key, I created a POCO composite key identifier called ResourcesInSectionIdentifier, and also defined an Equals(object obj) method and a GetHashCode method.

When I use NHibernate Session to save a new entry for the ResourcesInSectionBridge table then I get an error:

Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint “FK_Resources_Resou__22CA2527″. The conflict occurred in database “perls”, table “dbo.Resources”, column ‘ResourceDatabaseID’.
The statement has been terminated.

The mapping for the AllResourcesInfo.hbm.xml is as follows:

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

The mapping for the Section.hbm.xml is as follows:

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

Finally, our ResourcesInSectionBridge.hbm.xml mapping file is as follows:

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

Finally, our C# code that tries to create an entry into the ResourcesInSectionBridge table is as follows:

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())

When I use NHibernate Session to save a new entry for the ResourcesInSectionBridge table then I get an error:

Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint “FK_Resources_Resou__22CA2527″. The conflict occurred in database “perls”, table “dbo.Resources”, column ‘ResourceDatabaseID’.
The statement has been terminated.

Could you please explain why this error gets thrown? Also, could you please suggest how I could go about resolving the issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:44:47+00:00Added an answer on June 18, 2026 at 8:44 am

    The error you’re seeing is from the database and it says that the value for ResourceDatabaseID key doesn’t exist in the Resources table. Any entry made to bridge table has to be after the two source table have had their items insert if they don’t already exist.

    You need to properly populate both the Resource and Section entities for an insert into the bridge table to work. Look at the sample code for many-to-many mappings from these sample for something similar to what you’re trying to achieve. You can download the samples without requiring their product.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a spreadsheet that our office uses for scheduling. Each tab represents a
I have a dev server in our office that is behind the firewall. The
Our .NET team works on projects for our company that fall into distinct categories.
In our office we have a 40 LCD mounted to the wall that runs
I have just installed dexterity into our office's intranet site so that I can
I work in a small iPhone development team, in our office, we have at
We have a local web server in our office that we use for some
We have a question we ask at our office during interviews that goes like
I have a test server that sits in the corner of our office. I
I am working on revising our office intranet site from .ASP under IIS5 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.