Data Migration and EasyObjects

by Matthew Noonan 20. February 2008 00:33

I just wanted to post this code, because sometimes I find it hard to believe that things can go so smoothly when using EasyObjects and MyGeneration. Quite simply, they rock!

I needed to migrate the Comments from the old DNN blog to the BlogEngine database. Although I could have done it using T-SQL, the code would have been unwieldy and hard to debug. So instead I chose to whip up some code using EasyObjects. It literally took 20 minutes and worked on the first try, no debugging required. I can't promise you exactly the same results with your project, of course, but it is a good example of how quickly you can develop database applications once you become comfortable with the tools you use.

By request, I can make the code available for download, although I doubt it would be of much use to anyone because it is very specific (DNN to BlogEngine). Sorry for the lack of comments in the code, but I don't plan on using the code ever again, so why take the time?

static void Main(string[] args)
{
    Blog_Comments oldComment = new Blog_Comments();
    oldComment.Where.Title.Value = "Cialis";
    oldComment.Where.Title.Operator = WhereParameter.Operand.NotContains;
    oldComment.Query.AddOrderBy(Blog_CommentsSchema.EntryID);
    
    oldComment.Query.Load();

    PostComment newComment = new PostComment();
    do
    {
        Blog_Entries oldEntry = new Blog_Entries();
        oldEntry.LoadByPrimaryKey(oldComment.EntryID);

        Posts newEntry = new Posts();
        newEntry.Where.Title.Value = oldEntry.s_Title;
        newEntry.Query.Load();

        newComment.AddNew();
        newComment.s_Author = oldComment.s_Author;
        newComment.s_Comment = oldComment.s_Comment;
        newComment.s_CommentDate = oldComment.s_AddedDate;
        newComment.s_Country = "us";
        newComment.s_Email = "noreply@noonanconsultinginc.com";
        newComment.s_Ip = "127.0.0.1";
        newComment.IsApproved = true;
        newComment.s_PostID = newEntry.s_PostID;
        newComment.s_Website = "http://www.easyobjects.net";

    } while (oldComment.MoveNext());

    Console.WriteLine(newComment.ToXml());
    newComment.Save();

    Console.WriteLine("\n\nPress <Enter> to continue...");
    Console.ReadLine();
}

 

And here is the complete list of files:

BlogCommentXfer

Remember, that's going from a blank project to a fully-functional table migration in 20 minutes, including dynamic SELECT queries, table-to-table column mapping, null-value handling, looping through results and a batch INSERT at the end (by calling Save()). If I had thought of it, I could have added a transaction wrapper around the whole thing in another minute. In fact, it has almost taken me as long to write this blog post as it did to write the code and migrate the data.

Pretty damn cool.

kick it on DotNetKicks.com

Comments

3/5/2009 6:39:57 AM #

Good work..Nice post..thanks for sharing...

Pankaj United States

Comments are closed