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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:04:31+00:00 2026-06-05T14:04:31+00:00

I want to save a single object to the database twice. I have this

  • 0

I want to save a single object to the database twice. I have this code:

using (var ss = NHibHelp.OpenSession())
using (var tt = ss.BeginTransaction())
{
    var entity = new Entity();

    ss.Save(entity);
    ss.Save(entity);

    tt.Commit();
}

But this results in only one row being added to the database. How do I insert a single object into the database twice (with two different Ids) ?

  • 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-05T14:04:33+00:00Added an answer on June 5, 2026 at 2:04 pm

    You shouldn’t do this – NHibernate maintains “object identity” within it’s session, so it will not differentiate between ..well.. the same object. I would really advise against this, and a better solution would be to look at a way of cloning the object (either via reflection, or a Clone method), and then saving the cloned object.

    If you want to ignore my advice above, you can get it to work by evicting the entity from the session, setting it’s id back to it’s unsaved value (depends on your mapping, but probably 0), and then saving it again.

    It might also work if you just called session.Merge(entity) twice (you probably have to reset the id to it’s unsaved value after the first call).

    Alternatively you could use a stateless session with session.Merge() and then you don’t have to evict the entity between Save’s.

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

Sidebar

Related Questions

I want to be able to save any C# object in a single column
I have an array, I want to save this in UITable, but it is
I have a persistent hibernate object I obtained using session.save(object) I changed it since.
We have XML code stored in a single relational database field to get around
I want to save a set of queries (multiple SQL updates) as a single
I want to save an high-score for local use only. A single int, that
I want to save and load my xml data using XmlReader. But I don't
I have the following question: It is easy to insert an oBject in database
Intro I have an object @organization that has_many :quick_facts Basically, I want to produce
I have a system with several steps. Each step increments one single object instance.

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.