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

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

From the database, I created both the Entity Framework model construct and the DataSet model using Visual Studio designers. The DataSet is intended for bulk data import from SqlBulkCopyafter a rather complicated parsing process. All requests are then executed using the Entity Framework model, CreateQuerywhose method is opened through an interface similar to 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 that case I would like to omit the import into the database, but just have a view in the data memory accessible as Entities. The idea is to create a DataSet, but instead of bulk import, directly pass it to an ObjectContext accessible through the interface.

It makes sense?

Now here's what I did for this conversion: I move all the tables to a DataSet, convert individual rows into entities of the appropriate type, and add them to an object instance of my typed Entity context class, for example

            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
            ///....

Very tedious work, since I will need to create a converter for each of my entity types and iterate over each table in the DataSet that I have. Beware if I ever change my database model ....

, , MyEntities, SQL Server. , . - .

? - , , ObjectContext DataSet?

P.S: , , .

+3
1

, , automapper. .

, , , .

, , , EF 4 POCO.

, .

, , - "Map":

  • DTO EF
  • EF DTO

, .

+1

Source: https://habr.com/ru/post/1742296/


All Articles