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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:25:09+00:00 2026-05-15T11:25:09+00:00

I am facing an unusual behaviour with the Repository and Transactions and its making

  • 0

I am facing an unusual behaviour with the Repository and Transactions
and its making me mad.

I have two simple POCO classes Action and Version as follows. There is
a one to many relationship from Action->Version.

public class Action : Entity
{
    public virtual string Name
    {
        get;
        set;
    }

    public virtual IList<Version> Versions
    {
        get;
        protected set;
    }

    public Action()
    {
        Versions = new List<Version>();
    }

    public virtual void AddVersion( Version version )
    {
        version.Action = this;
        Versions.Add( version );
    }
}

public class Version : Entity
{

    public virtual int Number
    {
        get;
        set;
    }

    public virtual Action Action
    {
        get;
        set;
    }

    public Version()
    {
    }
}

Now I have a code segment as follows:

    var actionRep = new Repository<Action>();
    var versionRep = new Repository<Version>();

    var act1 = new Action( );
    actionRep .SaveOrUpdate( act1 );

    using (versionRep .DbContext.BeginTransaction())
    {

        var v1 = new Version();
        act1.AddVersion( v1 );
        //versionRep .SaveOrUpdate( v1 );
        versionRep .DbContext.CommitTransaction();
    }

In the above code I am just creating Action and Version repositories.
First I persist an Action object using Action Repository. Then I begin
a transaction using Version repository , create a new version, set
references with the Action, and Commit the transaction without
actually calling Version Repository.

The result is a bit strange. The version object gets persisted even
though I have not called SaveOrUpdate method on version repository. If
I comment out the line act1.AddVersion( v1 ); within the transaction,
then the Version does not get persisted.

After a bit of struggle I tested the same scenario using NHibernate
directly instead of using Sharp Architecture repositories using same
fluent mapping/configuration
(AutoPersistenceModelGenerator.Generate()). And The result is as
expected. The version object does not get persisted. Here is the code

    var sessionFactory = CreateSessionFactory();
    _act1 = new Action();

    using( var session = sessionFactory.OpenSession() )
    {
        session.SaveOrUpdate( _act1 );
    }

    using( var session = sessionFactory.OpenSession() )
    {
        using (var transaction = session.BeginTransaction())
        {
            _v1 = new Version();
            _act1.AddVersion( _v1 );
            //session.SaveOrUpdate( _act1 );
            transaction.Commit();
        }
    }

The CreateSessionFactory() method is as follows. Nothing complicated

        private const string _dbFilename = "nhib_auditing.db";
        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database( SQLiteConfiguration.Standard
                            .UsingFile( _dbFilename )
                            .ShowSql())
                .Mappings( m => m.AutoMappings.Add( new
AutoPersistenceModelGenerator().Generate() ) )
                .ExposeConfiguration( BuildSchema )
                .BuildSessionFactory();
        }

Now If sombody could please let me know why I am having this
behaviour. Its making me mad.

Just so that you know I have not overridden mapping for Action and
Version either.

Awaiting
Nabeel

  • 1 1 Answer
  • 1 View
  • 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-15T11:25:10+00:00Added an answer on May 15, 2026 at 11:25 am

    This is the expected behavior if you have cascading operations declared in the mapping. When you called SaveOrUpdate on act1 you made the transient object persistent; that is, NHibernate is tracking it and will save it when the session is flushed. Another way to make an object persistent is to associate it with a persistent object, as you did when you called act1.AddVersion( v1 );. The session was flushed when the transaction was committed so v1 was saved. This article explains the details of working with persistent objects.

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

Sidebar

Related Questions

I am facing an issue with this simple jQuery code. I have attached the
Am facing thi really unusual problem with MKNetworkKit caching. In simple word's the problem
Been Facing this issue,I Have as my view <%= form_for(:pin, :url => {:action =>fees})
I facing a weird problem here. I already have a simple try-out for jquery
Facing some problems with jQuery selectors. My HTML is like: <form method= action=> <p
I am facing a curious problem. I have a couchApp hosted on Iris couch.
I am facing one unusual behavior of curl. For a given page, I some
facing some strange behavior in OSB, i have configured WLS with MQ in client
Im facing a problem. I have a listing page in which i list all
I am facing an unusual problem in Oracle SQL Developer. When I try to

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.