Smart    Lucky

Smart Lucky

  • NA
  • 555
  • 637.8k

Nhibernate Session Issue When Inserting Data in Other DB

Feb 20 2014 5:15 AM
Hi dudes
 i am using nhibernate here i want get records from one database and insert them in an other multiple databases i have make following code but it is giving me exception regarding session can nay one help me please how can i do this....
namespace CopyCoupons
{
  public class Program
  {
   
  static void Main(string[] args)
  {
  ServiceContextCopyCoupon _contextSource;
  ServiceContextCopyCoupon _contextDestination;
  NHibernateHelperForConsole _myHelper;
  var successFullMovedCouponId=new List<string>();
  var unSuccessFullMovedCouponId = new List<string>();
   
  try
  {
  string[] connections=new[] {"Data Source=.;Initial Catalog=22;Integrated Security=true;","Data Source=.;Initial Catalog=23;Integrated Security=true;","Data Source=.;Initial Catalog=25;Integrated Security=true;"};
  var myHelperDefault = new NHibernateHelperForConsole();
  myHelperDefault.InitializeSessionFactory();
  _contextSource = new ServiceContextCopyCoupon(myHelperDefault);
  var coupnsForMove = _contextSource.couponService.Query().Where(x => x.Ismove == false).ToList();
   
  //CurrentSessionContext.Unbind(myHelperDefault.SessionFactory);

  foreach (string conn in connections)
  {
  successFullMovedCouponId.Add(conn);
  unSuccessFullMovedCouponId.Add(conn);
  _myHelper = new NHibernateHelperForConsole();
  _myHelper.InitializeSessionFactory(conn);
  _contextDestination = new ServiceContextCopyCoupon(_myHelper);
  foreach (var coupon in coupnsForMove)
  {
  using (var s = _myHelper.SessionFactory.OpenSession())
  {
  var company2 = _contextDestination.companyService.Query().FirstOrDefault(x => x.Id1 == coupon.Companyid.Id1 || x.Id2 == coupon.Companyid.Id2 || x.Name2 == coupon.Companyid.Name2);
  }
  // Session session = sessionFactory.openSession();
  var company =_contextDestination.companyService.Query().FirstOrDefault(x =>x.Id1 == coupon.Companyid.Id1 ||x.Id2 == coupon.Companyid.Id2 ||x.Name2 == coupon.Companyid.Name2);
  coupon.Companyid = company;
  coupon.CreateDate = DateTime.Now;
  coupon.Ismove = true;
  _contextSource.CloseSession();
  myHelperDefault.SessionFactory.Close();
  if (_contextDestination.couponService.CreateCoupon(coupon))
  {
  successFullMovedCouponId.Add(coupon.id.ToString());
  }
  else
  {
  unSuccessFullMovedCouponId.Add(coupon.id.ToString());
  }
  }
  }
  }
  catch (Exception e)
  {
  Console.WriteLine(e.Message);
  }
  Console.ReadKey();
  }
  }
  public class ServiceContextCopyCoupon
  {
   
  public CouponService couponService { get; private set; }
  public CouponService CouponService { get { return couponService; } }

  public CategoryService categoryService { get; private set; }
  public CategoryService CategoryService { get { return categoryService; } }

  public CompanyService companyService { get; private set; }
  public CompanyService CompanyService { get { return companyService; } }

  public ISession _session;
  public ServiceContextCopyCoupon(NHibernateHelperForConsole pro)
  {
  //if (CurrentSessionContext.HasBind(pro.SessionFactory))
  // CurrentSessionContext.Unbind(pro.SessionFactory);
  _session = pro.SessionFactory.OpenSession();
  categoryService = new CategoryService(_session);
  companyService = new CompanyService(_session);
  couponService = new CouponService(_session);
  }
  public void CloseSession()
  {
  if (_session != null && _session.IsOpen)
  {
  _session.Dispose();
  }
  }
  }
}

   public class NHibernateHelperForConsole
    {
        public ISessionFactory _sessionFactory;
        public ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                    this.InitializeSessionFactory();
                return this._sessionFactory;
            }
        }
        public NHibernateHelperForConsole() { }
        public void InitializeSessionFactory(string connectionString = "Data Source=.;Initial Catalog=24;Integrated Security=true;")
        {
           this._sessionFactory = Fluently.Configure()
                 .Database(MsSqlConfiguration.MsSql2008
                               .ConnectionString(connectionString)
                 )
                 .Mappings(m =>
                           m.FluentMappings
                               .AddFromAssemblyOf<Category>())
                 .ExposeConfiguration(cfg => new SchemaExport(cfg)
                 .Create(false, false))
                 .BuildSessionFactory();
            //.Conventions.Add( FluentNHibernate.Conventions.Helpers.DefaultLazy.Never() )
        }



     
      

    }