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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:27:41+00:00 2026-06-01T08:27:41+00:00

I’m having trouble with the copying entities between multiple databases. I can’t seem to

  • 0

I’m having trouble with the copying entities between multiple databases. I can’t seem to get my head around this issue and really need some help with the implementation.

My current implementation is described here:

Http module

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using ISMSControl.Infrastructure.Sessions;
using NHibernate;
using NHibernate.Context;

namespace ISMSControl.Infrastructure.Modules
{
    public class SessionModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += OpenSession;
            context.EndRequest += CloseSession;
        }
        private void CloseSession(object sender, EventArgs e)
        {
            ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.GetCurrentSession().SessionFactory);

            if (session != null)
            {
                if (session.Transaction != null && session.Transaction.IsActive)
                    session.Transaction.Rollback();
                else
                    session.Flush();

                session.Close();
            }
        }
        private void OpenSession(object sender, EventArgs e)
        {
            ManagedWebSessionContext.Bind(HttpContext.Current,
                SessionManager.GetCurrentSession());
        }
        public void Dispose()
        {
        }
    }
}

SessionManager implemenation

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using ISMSControl.Infrastructure.Mappings;
using NHibernate;
using NHibernate.Cache;

namespace ISMSControl.Infrastructure.Sessions
{
    public sealed class SessionManager
    {
        private const string CurrentSessionKey = "nhibernate.current_session";
        private static readonly ISessionFactory sessionFactory;

        static SessionManager()
        {
            sessionFactory = CreateSessionFactory("source");
        }

        private static ISessionFactory CreateSessionFactory(string connectionStringName)
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ShowSql().ConnectionString(c => c.FromConnectionStringWithKey(connectionStringName)))
                .CurrentSessionContext("managed_web")
                .Cache(c =>
                {
                    c.UseQueryCache();
                    c.ProviderClass<HashtableCacheProvider>();
                })
                .Diagnostics(d =>
                {
                    d.Enable();
                    d.OutputToConsole();
                })
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<StandardMapping>())
                .BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] = currentSession;
            }

            return currentSession;
        }
        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                // No current session
                return;
            }
            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);
        }
        public static void CloseSessionFactory(string sessionFactoryName = null)
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }
}

Repository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Web;
using ISMSControl.Domain;
using ISMSControl.Domain.Contracts;
using ISMSControl.Infrastructure.Sessions;
using NHibernate;
using NHibernate.Context;

namespace ISMSControl.Infrastructure.Repositories
{
    public class StandardRepository : IStandardRepository
    {
        public void SaveOrUpdate(Standard standard)
        {
            var session = SessionManager.GetCurrentSession();

            using (var transaction = session.BeginTransaction())
            {
                session.SaveOrUpdate(standard);
                transaction.Commit();
            }
        }
        public IEnumerable<Standard> RetrieveList()
        {
            return SessionManager.GetCurrentSession().CreateCriteria<Standard>().List<Standard>();
        }
        public void CopyTo(string database, Standard standard)
        {
            //how do i implement this method, so it will copy the standard entity to the other database?
        }
    }
}

The problem is that i’m getting all these different kind of errors like, “The session is closed.”, “The entity belows to another transaction or something”. “Illegal attempt to associate a collection with two open sessions”.

I really hope that someone can help me out our point me in the right direction by sharing a

  1. Tutorial
  2. Example
  3. etc.

CopyTo implemenation

public void CopyTo(string sessionFactoryName, Standard standard)
    {
        //gets a new session for the destination database from the destination sessionfactory.
        using (var destinationSession = SessionFactoryContainer.Current.Get(sessionFactoryName).OpenSession())
        {
            //error: no persister for...
            var newStandard = new Standard();
            newStandard.Code = standard.Code;
            newStandard.Description = standard.Description;
            newStandard.Explanation = standard.Explanation;

            destinationSession.Save(newStandard);
        }
    }
  • 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-01T08:27:42+00:00Added an answer on June 1, 2026 at 8:27 am

    In your “CopyTo” method, you have to create a session on the second database, deep clone the second parameter of your method and then attach the cloned object to the session you opened.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build

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.