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 3633678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:41:00+00:00 2026-05-19T00:41:00+00:00

I have a database with 3 tables: Project Sponsor ProjectSponsor In my domain objects,

  • 0

I have a database with 3 tables:

  1. Project
  2. Sponsor
  3. ProjectSponsor

In my domain objects, i have an object Project which has a child property called Sponsors which is a list of ProjectSponsor objects, like this:

 IList<ProjectSponsor> Sponsors;

here is the Nhibernate Mapping code:

ProductMap:

HasMany(x => x.Sponsors).AsBag().Inverse().Cascade.AllDeleteOrphan();

ProductSponsorMap:

References(x => x.Project).Not.Nullable();
References(x => x.Sponsor).Not.Nullable();

here is my Domain Objects:

public class Project
{
    public virtual IList<ProjectSponsor> Sponsors { get; set; }
}

 public class ProjectSponsor
{
    public virtual Project Project { get; set; }
    public virtual Sponsor Sponsor { get; set; }
}

ok . . now to my issue, when i try to add new Projects (with their associated projectsponsor) into the database.

Here is my code:

    foreach (Project project in newProjects)
   {
      Repository.Save(project);
      foreach (ProjectSponsor projectSponsor in project.Sponsors)
      {
           Repository.Save(projectSponsor);
      }
   }

I am getting an exception on this line:

     Repository.Save(projectSponsor);

saying that not-null property reference a null or transient value is trying to be saved:

projectSponsor.Project is the property that its complaining about. I would have thought that would be set by default based on the nhibernate mapping.

do i really need to have an explicit piece of code that have this code:

projectSponsor.Project = project

or is there anything wrong with my mapping files ?

  • 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-19T00:41:00+00:00Added an answer on May 19, 2026 at 12:41 am

    If you haven’t set the projectSponsor.Project property (and no code here says you have) then it will have the default value of null which will violate the mapping condition you’ve placed.

    NHibernate is just going to reflect the state of the objects and the state of the objects is – for want of a better word – broken as is. You could remove the Project property on the ProjectSponsor map and all would be well but you’re losing functionality to do so because obviously the directionality of relationships in OO is the reverse of RDBM directionality.

    I would expect something like:

    public class ProjectSponsor 
    {     
        // This is how you instantiate me!
        public ProjectSponsor(Project project, Sponsor sponsor)
        {
            this.Project = project;
            this.Sponsor = sponsor;
        }
    
        protected ProjectSponsor()
        {
            // I exist for NHibernates benefit only!
        }
    
        public virtual Project Project { get; set; }     
    
        public virtual Sponsor Sponsor { get; set; } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that necessarily spans several databases. One database has tables: CREATE
I have database project in visual studio 2010. It has tables views stored procedures
I have a project here that connects to an in-production database and grabs tables
I am designing a database for a project. I have a table that has
I have built a database on multiple tables. One of these tables has a
I have a database that's pre-populated with tables from another project.. How do I
I have some database tables named Project , Employee and Branch . An employee
I have a Visual Studio 2005 BIDS project that exports about 30 database tables
I have database schema for an integration project in which I need to be
I Have a DataBase in my project With Table named 'ProcessData' and columns named

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.