Migrating data from one database server to another with Telerik OpenAccess ORM

Posted by: the telerik blogs, on 23 Oct 2009 | View original | Bookmarked: 0 time(s)

In todays post we are going to discuss a topic which is known to be fairly complex in most cases, but it is relevantly easy to be achieved with Telerik OpenAccess ORM.


Let us consider the following scenario: your company decides to move your data from one database server to another, say from MSSQL Server instance to MySql. The schema is the same on both servers but the data resides inside the MSSQL Server instance. What follows is the easiest way to move all the data from the MSSQL server database to the MySql one:


The class that is crucial to successfully execute such data transport with Telerik OpenAccess ORM is [not surprisingly] the Object Container. The biggest reason for this fact is that the Object Container is Serializable. The object container is perfect for holding persistent data in a serialized format. This data can then easily be extracted into an object scope that belongs to a different database with the same schemata. When the object scope and object container exchange data, usually they know about the state of the objects they exchange. This is the reason for the existence of the MarkContentAsNew() method of the container. In order to transfer the objects contained in the ObjectContainer to the second database object scope, the persistent objects must be marked as NEW, which is exactly what this method does to the container's content.


Here is the sample code presenting this part of the operation. We are intending to migrate the content of Northwind database from MSSQL Server to MySql.

using (IObjectScope scope = Database.Get("MSSQLServerConnection").GetObjectScope())
            {
                FetchGroupCollector collector = new FetchGroupCollector(FetchGroupCollector.DeepFetchGroup);
                Type[] types = new Type[]{typeof(Category),typeof(Customer),typeof(Employee),
                    typeof(Order),typeof(OrderDetail),typeof(Product),typeof(Region),
                    typeof(Shipper),typeof(Supplier),typeof(Territory)};
                scope.Transaction.Begin();
                foreach (Type t in types)
                {
                    IQueryResult objects = scope.GetOqlQuery("SELECT x FROM " + t.Name + "Extent as x").Execute();

                    container.CopyFrom(scope, t.Name, objects, collector);
                }
                scope.Transaction.Commit();

            }

            container.MarkContentAsNew();
            Telerik.OpenAccess.ObjectContainer.ChangeSet set = container.GetChanges(ObjectContainer.Verify.All);
            Stream str = new FileStream("C:\\container.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(str, set);

 

If this content is then applied to an object scope that represents the empty database on the new server, the object scope is just filling the database with that content as if you were adding it via the normal procedure.

Below is the code performing this:

            using (IObjectScope scope = Database.Get("MySqlConnection").GetObjectScope())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                Stream str = new FileStream("C:\\container.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);

                Telerik.OpenAccess.ObjectContainer.ChangeSet cs = 
                    (Telerik.OpenAccess.ObjectContainer.ChangeSet)formatter.Deserialize(str);

                ObjectContainer.CommitChanges(cs, ObjectContainer.Verify.All, scope, true, false);

            }
 

That is all about it. This has definitely proved useful on our side, and I hope some of you will find it beneficial as well.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: SQL | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1131 | Hits: 138

Similar Posts

  • Using WCF with SQL Azure and Telerik OpenAccess more
  • Connecting to SQL Azure with Telerik OpenAccess more
  • How to display data from different tables using one data source more
  • Telerik OpenAccess WCF Wizard October CTP more
  • Binding Hierarchical RadGrid for ASP.NET Ajax with Telerik OpenAccess ORM more
  • Binding Hierarchical RadGrid with Telerik OpenAccess ORM more
  • An alternative to Crystal more
  • Using the Telerik OpenAccess WCF Wizard with Multiple Versions of OpenAccess more
  • Tracing the SQL Statements Generated by Telerik OpenAccess ORM more
  • WPF Release History : Q2 2009 (version 2009.2.701) more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD