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

  • Home
  • SEARCH
  • 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 1082313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:16:50+00:00 2026-05-16T22:16:50+00:00

I have two classes mapped using fluent NHibernate – User and a UserRoleAssignment. An

  • 0

I have two classes mapped using fluent NHibernate – User and a UserRoleAssignment. An User has many UserRoleAssignments. Here’s the relevant map for user:

HasMany(x => x.Roles)
.Table("UserRoleMap")
.Cascade.SaveUpdate();

And for UserRoleAssignment:

Map(x => x.User_Id);

As you can see, I’m only referencing from User to UserRoleAssignment. From UserRoleAssignment, I’m only mapping the foreign key column (User_Id).
The problem is, that when I save the user, I get an exception caused by the foreign key constraint, because NHibernate is inserting 0 in the value for the User_id property. This only happens on new users – existing users (which already has an ID), works fine.

So the question is, how can I make sure that NHibernate sets the value of User_Id to the ID generated when inserting the new User?

Thanks a lot!

  • 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-05-16T22:16:50+00:00Added an answer on May 16, 2026 at 10:16 pm

    This is documented in http://nhibernate.info/doc/nh/en/index.html#collections-onetomany:

    Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter.

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

Sidebar

Related Questions

There are two classes: User and Question A user may have many questions, and
I have two classes binded mapped with Hibernate and I can't figure out the
I have two C# classes that have many of the same properties (by name
I have two classes Foo and Bar mapped to two different tables, and I'd
I'm using Hibernate version 3.3.2.GA with annotations. I have inheritance between two classes, the
I have two classes, and want to include a static instance of one class
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes that each need an instance of each other to function.
I have two classes: Media and Container. I have two lists List<Media> and List<Container>
I have two classes A and B in two different .NET assemblies: AssemblyA and

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.