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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:01:24+00:00 2026-05-26T06:01:24+00:00

I am using NHibernate 3.2, and using Mapping by Code. The test case is

  • 0

I am using NHibernate 3.2, and using Mapping by Code.

The test case is pretty straight-forward – I have a Store and a Product, linked by a many-to-many collection. The many-to-many collection in the StoreMap is mapped as follows:

Set(x => x.Products, x =>
        {
            x.Fetch(CollectionFetchMode.Subselect);
            x.Key(key =>
            {
                key.Column("StoreId");
                key.ForeignKey("FK_StoreProducts_Store");
            });
            x.Table("StoreProducts");
            x.Cascade(Cascade.None);
            x.Inverse(true);

        },

If I am correct, the Cascade option should allow you to choose whether you want to cascade operations on the class, to the collection as well. NHibernate 3.2 allows these options, which are not really straightforward for me:

[Flags]
public enum Cascade
{
    None = 0,
    Persist = 2,
    Refresh = 4,
    Merge = 8,
    Remove = 16,
    Detach = 32,
    ReAttach = 64,
    DeleteOrphans = 128,
    All = 256,
}

The example I am using is the one below, where I am creating a test Store, and a test Product, and assigning it to the store. Then I am reloading the Store, retrieving the first Product, updating it, and saving the Store.

Even though the cascade is set as ‘None’, the Product still updates! Is it normal that this should happen?

using (var session = SessionFactory.OpenSession())
        {
            long storeId = 5;
            using (var t = session.BeginTransaction())
            {
                Product p1 = new Product();
                p1.Name = "Product 1";
                session.Save(p1);

                Store store = new Store();
                store.Name = "Tesco";
                session.Save(store);

                p1.Stores.Add(store);
                store.Products.Add(p1);
                session.Save(store);

                storeId = store.Id;
                t.Commit();
            }

            using (var t = session.BeginTransaction())
            {
                Store s = session.Get<Store>(storeId);
                Product p = s.Products.FirstOrDefault();
                p.Name = "Product 1 Updated";
                session.Save(s);
                t.Commit(); // <-- Product is still being updated here!

            }
            session.Flush();
        }
  • 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-26T06:01:24+00:00Added an answer on May 26, 2026 at 6:01 am

    Feature that you are facing is called change tracking, and has nothing to do with cascading options. NHibernate tracks all objects that are associated with a session and sends all changes to DB when transaction is committed.

    If you don’t need it you can use Stateless session in order to get non tracked entities. But in this case you will not have lazy loading. Also you can try to use FlushMode.Never in order to prevent NHibernate from automatic flushing changes to DB

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

Sidebar

Related Questions

Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
I'm using Fluent NHibernate, and auto-mapping the classes. I have a computed property in
I'm using mapping by code in NHibernate. I got a class with several properties.
I'm using Fluent NHibernate auto mapping. I need to access more than one database
I'm using NHibernate and Fluent NHibernate to create a mapping file for a domain
Can someone tell me how to accomplish this mapping using Fluent NHibernate? It's simply
I am new to nHibernate. I understand how to use mapping using Fluent nHibernate.
I have the following Hibernate Mapping, which has to be mapped using the Table
Just started developing a project using NHibernate and Fluent NHibernate for the mapping and
I have 2 tables Main and Maintest. I am using nhibernate to pull data

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.