One-to-Many Update with Fluent NHibernate

I am trying to create a modified role system that has the following class / relationship structure:

Project ProjectRole Employee

Where

ProjectEmployee maps three objects together.

I have a one-to-many mapping from Project to ProjectEmployee and a link mapping from ProjectEmployee to Project.

I can add a new ProjectEmployee just fine (which means my Create actions are fine too), but I want to limit the relationship to only one record per employee. This affects the logic of my update actions.

Hacking this together, I am trying to clean up Project.ProjectEmployees (by setting the value to a new list) and then adding a new ProjectEmployee instance with the appropriate role, etc.

When doing so, there is an error related to setting Project.ProjectEmployees to an empty list.

"Unable to insert NULL into column 'ProjectId', table 'AppDB.dbo.ProjectEmployees', column does not allow zeros. UPDATE failed. Application completed.

If I remove the line containing the news in the list, further entries are added, but then I look at too many entries per employee.

Here's my Project -> ProjectEmployees mapping:

mapping.HasMany<ProjectEmployee>(x => x.ProjectEmployees)    
    .Cascade.All()
    .WithKeyColumn("ProjectId");

      

And the mapping to ProjectEmployee -> Project:

mapping.References(x => x.Project, "ProjectId");

      

Should be just right?

For which I also added a method to the Project class called AddProjectEmployee that looks for the appropriate employee, removes its ProjectEmployee if it exists, and then adds a new ProjectEmployee. I am getting the same error when I call it.

Any help would be appreciated.

Nick

+1


a source to share


1 answer


This was a cross reference to the Fluent NHibernate group and answered there by Hudson Akridge. As Hudson said, the solution is to use Clear()

on a collection, as this allows NHibernate to track changes (whereas creating a new collection causes the property to reference an NH instance that it doesn't know about).



+1


a source







All Articles