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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:49:25+00:00 2026-05-20T02:49:25+00:00

In my asp.net mvc app I have a Log object which tracks what happens

  • 0

In my asp.net mvc app I have a Log object which tracks what happens in my forum. when a comment is added inside a transaction the following happens:

  1. A new comment is added to the submissions table

  2. A new log entry with a reference to the id of that comment is added to the logs table

Finally the transaction is committed and no exceptions are generated. But it turns out that when the transaction is committed, NHibernate first inserts the Log record and then the Comment record, therefore saving the log record with an incorrect comment id which is 0.

Here is some code to show what is happening:

using (IAtomicTransaction Transaction = UnitOfWork.BeginTransaction())
{
    try
    {
        SubmissionRepository.AddComment(Comment, ParentSubmission);
        LogRepository.AddCommentEntry(Comment);            

        Transaction.Commit();
    }
    catch
    {
        Transaction.Rollback();
        throw;
    }
}

UnitOfWork and AtomicTransaction are just wrappers around the ISession and ITransaction objects from the NHibernate APIs. And here is the log generated which confirms the problem:

2011-02-09 14:42:05,631 [15] DEBUG NHibernate.SQL - INSERT INTO logs (version, created_at, updated_at, comment_id) VALUES (?p0, ?p1, ?p2, ?p3); ?p0 = 9/02/2011 2:42:05 PM, ?p1 = 9/02/2011 3:41:52 AM, ?p2 = NULL, ?p3 = 0
2011-02-09 14:42:05,647 [15] DEBUG NHibernate.SQL - SELECT LAST_INSERT_ID()
2011-02-09 14:42:05,647 [15] DEBUG NHibernate.SQL - INSERT INTO submissions (version, created_at, updated_at, body) VALUES (?p0, ?p1, ?p2, ?p3);?p0 = 9/02/2011 2:42:05 PM, ?p1 = 9/02/2011 3:41:52 AM, ?p2 = 9/02/2011 3:41:52 AM, ?p3 = 'dfgdfgd dfg df'
2011-02-09 14:42:05,647 [15] DEBUG NHibernate.SQL - SELECT LAST_INSERT_ID()

What is the solution to this problem?

Update:

So it turned out that if I call Flush after every insert then the order will be correct.

using (IAtomicTransaction Transaction = UnitOfWork.BeginTransaction())
{
    try
    {
        SubmissionRepository.AddComment(Comment, ParentSubmission);
        UnitOfWork.CurrentSession.Flush();

        LogRepository.AddCommentEntry(Comment);            
        UnitOfWork.CurrentSession.Flush();

        Transaction.Commit();
    }
    catch
    {
        Transaction.Rollback();
        throw;
    }
}
  • 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-20T02:49:26+00:00Added an answer on May 20, 2026 at 2:49 am

    Sure, when you try to add a comment and a log entry in a single transaction the ID of Comment isn’t yet generated, unless you use “Manual ID assignment” approach or flush your session to persist changes you made.

    In your original post there is no “Flush” after you add the comment and you want to add the log for that comment (for which ID isn’t defined yet).

    Not sure about why Nhibernate takes such a reverse order but seems like its at least an ID generation issue.

    Another thing that caught my attention is that you use Comment and Args.Comment side by side. What is difference?

    Anyway, if Comment is somewhat your domain entity, you have to:

    • Insert it;
    • Make sure it is attached to the current ISession (you should have the session-attached Comment entity object available);
    • Use exatly that entity (which is in repository already) for log entry (NHibernate will take care of relations).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ASP.NET MVC app, I have a fairly complex edit page which combines
In my ASP.NET MVC app, I have an interface which acts as the template
Within an asp.net MVC app, I'm trying to find rooms which have a guest/client,
I am trying to make an ASP.NET MVC 3 app in which I have
I have an ASP.NET MVC App, which use EF code First, for some reason
I have an ASP.Net MVC app, and i have some xsl files inside of
I have developed a web app in ASP.NET MVC which we are using internally.
I have an ASP.NET MVC app which has a bootstrapper class that configures log4net
I have an asp.net mvc app which has membership implemented. So a user has
I have an ASP.Net MVC 3 app that I've developed which uses RavenDB Embedded

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.