How to convert a DataSet object into an ObjectContext (Entity Framework) object on the fly?

Posted by Marcel on Stack Overflow See other posts from Stack Overflow or by Marcel
Published on 2010-04-22T14:49:34Z Indexed on 2010/05/01 6:27 UTC
Read the original article Hit count: 1483

Hi all,

I have an existing SQL Server database, where I store data from large specific log files (often 100 MB and more), one per database. After some analysis, the database is deleted again.

From the database, I have created both a Entity Framework Model and a DataSet Model via the Visual Studio designers. The DataSet is only for bulk importing data with SqlBulkCopy, after a quite complicated parsing process. All queries are then done using the Entity Framework Model, whose CreateQuery Method is exposed via an interface like this

    public IQueryable<TTarget> GetResults<TTarget>() where TTarget : EntityObject, new()
    {
        return this.Context.CreateQuery<TTarget>(typeof(TTarget).Name);
    }

Now, sometimes my files are very small and in such a case I would like to omit the import into the database, but just have a an in-memory representation of the data, accessible as Entities. The idea is to create the DataSet, but instead of bulk importing, to directly transfer it into an ObjectContext which is accessible via the interface.

Does this make sense?

Now here's what I have done for this conversion so far: I traverse all tables in the DataSet, convert the single rows into entities of the corresponding type and add them to instantiated object of my typed Entity context class, like so

            MyEntities context = new MyEntities(); //create new in-memory context
            ///....
            //get the item in the navigations table
            MyDataSet.NavigationResultRow dataRow = ds.NavigationResult.First(); //here, a foreach would be necessary in a true-world scenario
            NavigationResult entity = new NavigationResult
            {
                Direction = dataRow.Direction,
                ///...
                NavigationResultID = dataRow.NavigationResultID
            }; //convert to entities

            context.AddToNavigationResult(entity); //add to entities
            ///....

A very tedious work, as I would need to create a converter for each of my entity type and iterate over each table in the DataSet I have. Beware, if I ever change my database model....

Also, I have found out, that I can only instantiate MyEntities, if I provide a valid connection string to a SQL Server database. Since I do not want to actually write to my fully fledged database each time, this hinders my intentions. I intend to have only some in-memory proxy database.

Can I do simpler? Is there some automated way of doing such a conversion, like generating an ObjectContext out of a DataSet object?

P.S: I have seen a few questions about unit testing that seem somewhat related, but not quite exact.

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework