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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:52:58+00:00 2026-06-13T22:52:58+00:00

I have been experimenting with Entity Framework 4.4, NHibernate 3.3.1.4000 and SQL Server and

  • 0

I have been experimenting with Entity Framework 4.4, NHibernate 3.3.1.4000 and SQL Server and I have noticed a difference when it comes to fixing up the relationships when you commit your changes, and I was wondering what is the best practice or if I’m doing something wrong.

Here’s what I tested. I have a classic Parent linked to n Children. I have 2 parents in the database with 20 children each. I load both parents, and I take the first child of the first parent and assign that child the second parent. I then commit the changes.

In EF, after I have saved, I can see that the count for the Children collection of both parents has been altered, so it fixed up the relationships.

However, when I do the same thing in NHibernate, the counts remain the same.

Here’s my code setup to reproduce the issue.

POCOs:

public class Parent
{
    public virtual int ParentId { get; set; }

    public virtual string Name { get; set; }

    public virtual IList<Child> Children { get; set; }

    public Parent()
    {
        Children = new List<Child>();
    }
}

public class Child
{
    public virtual int ChildId { get; set; }

    public virtual int ParentId { get; set; }

    public virtual string Name { get; set; }

    public virtual Parent Parent { get; set; }
}

NHibernate Parent mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="ConsoleApplication1"
                   namespace="ConsoleApplication1">
  <class name="Parent" table="Parents">
    <id name="ParentId" column="ParentId" />
    <property name="Name" column="Name" />
    <bag name="Children" cascade="all">
      <key column="ParentId"/>
      <one-to-many class="Child"/>
    </bag>
  </class>
</hibernate-mapping>    

NHibernate Child mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="ConsoleApplication1"
                   namespace="ConsoleApplication1">
  <class name="Child" table="Children">
    <id name="ChildId" column="ChildId" />
    <property name="Name" column="Name" />
    <many-to-one name="Parent" class="Parent" column="ParentId" not-null="true" />
  </class>
</hibernate-mapping>    

EF DbContext:

public class Entities : DbContext
{
    public DbSet<Parent> Parents { get; set; }

    public DbSet<Child> Children { get; set; }
}

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="dialect">
        NHibernate.Dialect.MsSql2005Dialect
      </property>
      <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
      </property>
      <property name="connection.connection_string">
        Data Source=localhost;Initial Catalog=MaintainRelationshipsNH;Integrated Security=True;
      </property>
    </session-factory>
  </hibernate-configuration>
  <connectionStrings>
    <add
      name="Entities"
      providerName="System.Data.SqlClient"
      connectionString="Server=localhost;Database=MaintainRelationshipsNH;Trusted_Connection=true;"/>
  </connectionStrings>
</configuration>

Script to create the tables and data:

create table Parents (
   ParentId INT not null,
   Name NVARCHAR(255) null,
   primary key (ParentId)
)

create table Children (
   ChildId INT not null,
   Name NVARCHAR(255) null,
   ParentId INT not null,
   primary key (ChildId)
)

alter table Children
    add constraint FK_Children_Parents
    foreign key (ParentId)
    references Parents

declare @idChild int        
insert into Parents (ParentId, Name) values (0, 'John');
set @idChild = 0
while @idChild < 20
begin
   insert into Children (ChildId, Name, ParentId) values (@idChild, 'Child ' + convert(nvarchar(2), @idChild), 0);
   set @idChild = @idChild + 1
end
insert into Parents (ParentId, Name) values (1, 'Julie');
while @idChild < 40
begin
   insert into Children (ChildId, Name, ParentId) values (@idChild, 'Child ' + convert(nvarchar(2), @idChild), 1);
   set @idChild = @idChild + 1
end

NHibernate test code:

System.Diagnostics.Debug.WriteLine("Test NHibernate:");

Configuration configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Parent).Assembly);

ISessionFactory sessionFactory = configuration.BuildSessionFactory();

Parent parent0, parent1;
using (ISession session = sessionFactory.OpenSession())
{
    using (ITransaction transaction = session.BeginTransaction())
    {
        parent0 = session.Load<Parent>(0);
        parent1 = session.Load<Parent>(1);
        System.Diagnostics.Debug.WriteLine("Before modifications and commit");
        System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
        System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);

        parent0.Children[0].Parent = parent1;

        transaction.Commit();
    }
}
System.Diagnostics.Debug.WriteLine("After modifications and commit");
System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);

Entity framework test code:

System.Diagnostics.Debug.WriteLine("Test Entity Framework:");

Parent parent0, parent1;
using (Entities entities = new Entities())
{
    parent0 = entities.Parents.Find(0);
    parent1 = entities.Parents.Find(1);
    System.Diagnostics.Debug.WriteLine("Before modifications and commit");
    System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
    System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);
    parent0.Children[0].Parent = parent1;

    entities.SaveChanges();
}
System.Diagnostics.Debug.WriteLine("After modifications and commit");
System.Diagnostics.Debug.WriteLine("Parent0 number of children: " + parent0.Children.Count);
System.Diagnostics.Debug.WriteLine("Parent1 number of children: " + parent1.Children.Count);

So basically with this test I can see the counts changing with EF but not with NHibernate. Am I doing something wrong with NH or do I have to manually manage every part of the relationships affected by my changes ? Thanks!

  • 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-13T22:52:59+00:00Added an answer on June 13, 2026 at 10:52 pm

    In NHibernate you would need to manually move them from one parent collection to another. I usually use Add or Remove methods in the parent classes to do this. Here is an example of these add or remove methods:

        public virtual void AddLine(OrderLine orderLine)
        {
            orderLine.Order = this;
            this.orderLines.Add(orderLine);
        }
    
        public virtual void RemoveLine(OrderLine orderLine)
        {
            this.orderLines.Remove(orderLine);
        }
    

    To reparent a child I would then do something like this:

        originalParent.RemoveLine(child);
    
        newParent.AddLine(child);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting recently with Silverlight, RIA Services, and Entity Framework using .NET
I have been experimenting with writing applications that use a local SQL Database to
I have been experimenting with the lightweight NiceDog PHP routing framework, which routes like
I have been experimenting with Firefox's Audio API to detecting silence in audio. (The
I have been experimenting with a lot of web development apps like Drupal, Moodle,
I have been experimenting with sending messages from two .NET Windows Forms applications using
I have been experimenting with TMask in Delphi 2010 and it seems to work
I have been experimenting some problems with the fully distributed version. First of all
So I have been experimenting with building a new site... http://zergxost.com/test.html - it will
I'm working on building a tree structure in MySQL and have been experimenting with

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.