LSTANCZYK
10/17/2014 - 3:00 PM

CopyDataToNewDatabase.cs

protected void CopyDataToNewDatabase()
{
	//get all data from current database
        //(database connection strored in other place and initialize by _unitOfWork)
	XPCollection<Item> items = new XPCollection<Item>(_unitOfWork);
	items.Load();

	//prepare new connection string to other database
	string fileLocation = System.Web.Hosting.HostingEnvironment.MapPath(@"~\App_Data\multi_language.db");
	string conn = DevExpress.Xpo.DB.SQLiteConnectionProvider.GetConnectionString(fileLocation);
	DevExpress.Xpo.Metadata.XPDictionary dict = new DevExpress.Xpo.Metadata.ReflectionDictionary();
	dict.GetDataStoreSchema(typeof(Item).Assembly);
	DevExpress.Xpo.XpoDefault.Session = null;
	DevExpress.Xpo.DB.IDataStore store = DevExpress.Xpo.XpoDefault.GetConnectionProvider(conn, DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema);

	//create new session with new connection
	using (UnitOfWork uow = new UnitOfWork(new DevExpress.Xpo.ThreadSafeDataLayer(dict, store), null))
	{
		//loap each data collection
		foreach (Item item in items)
		{
			//save to new tables in new database
			Item newItem = new Item(uow);
			newItem.ItemID = item.ItemID;
			newItem.SerialNumber = item.SerialNumber;
			newItem.Description = item.Description;
			newItem.Manufacturer = item.Manufacturer;
			newItem.Supplier = item.Supplier;
			newItem.ManufacturerCN = item.ManufacturerCN;
			newItem.Save();
		}
		uow.CommitChanges();
	}
}