Question

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.

Was it helpful?

Solution

There are tools that map between objects, such as automapper. This is a very good open source tool.

However, these tools sometimes have problems, for example generating duplicate entity keys, or problems when the structure of the objects being mapped are very different.

If you are trying to automate it, I think that there is a greater chance of it working if you use EF 4 and POCO objects.

If you end up writing the mapping code manually, I would move it into a seperate procedure with automated unit tests on it.

The way we do this is to create a static class with "Map" methods":

  • From DTO to EF object
  • From EF to DTO

Then write a test for each method in which we check that the fields were mapped correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top