3

I need to import a huge amount of data without db work interruption. So there are two tables: Data and DataTemp(they are identical). At first data is uploaded to the temp table and then tables are swapped by backing up Data table and renaming DataTemp to Data(this example is simplified - there much more than two tables). Entity Framework is used in this project.

So the question is: is it possible to use entity framework to use DataTemp without duplicating the Data table in the schema? Is there a way to edit final sql before executing it to temporary replace table names? OnModelCreating doesn't fit because it's called once but I need to use both tables at different times.

Thanks!

SergB
  • 91
  • 7

1 Answers1

2
public class Datum
{
    public int Id { get; set; }
}

public class DataContext : DbContext
{
    public DbSet<Datum> Data { get; set; }
}

public class DataTempContext : DataContext
{
    protected override void OnModelCreating(DbModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.Entity<Datum>().ToTable("DataTemp");
    }
}

EDIT

This is working for me.

Use of old:

        using (var context = new DataContext())
        {
            // illustrate original table name, Data
            Console.WriteLine(context.Data.ToString());
            Console.WriteLine();

            // Add some real data, for LinqPad
            context.Data.Add(new Datum());
            context.SaveChanges();
        }

SQL output by WriteLine:

            SELECT
                [Extent1].[Id] AS [Id],
                FROM [Data] AS [Extent1]            

Use of new:

        using (var context1 = new DataContext())
        using (var context2 = new DataTempContext())
        {
            // copy old table to new
            foreach (var d in context1.Data)
                context2.Data.Add(d);
            context2.SaveChanges();
        }

        using (var context = new DataTempContext())
        {
            // illustrate different table name, DataTemp
            Console.WriteLine(context.Data.ToString());

            Console.ReadKey();
        }

SQL output by WriteLine:

            SELECT
                [Extent1].[Id] AS [Id],
                FROM [DataTemp] AS [Extent1]
John Castleman
  • 1,552
  • 11
  • 12
  • Thanks. Tried this. OnModelCreating is not called and data is saved to context1 tables. – SergB Feb 20 '15 at 14:58
  • weird ... are you sure `context2` was initialized to a `DataTempContext`? I don't always attempt code I post using a compiler, but I did this time, and the problem _I_ had was that I wound up with Data.sdf and DataTemp.sdf SQL CE DB files, because the default connection string for DbContext does that: however, the generated SQL clearly showed `[DataTemp]` as the table name, so I was content that it worked. – John Castleman Feb 20 '15 at 15:23
  • @JohnCastleman to fix the default connectiong string error, you could call the base DbContext's constructor for the `DataTempContext` to set the connection string, ie: `public DataTempContext() : base("DataContext") { }` – Chris Curtis Feb 20 '15 at 18:44
  • Also, it might not like inheriting from `DataContext`. Not sure what EF is doing under the covers, but making an abstract class both contexts inherit from could make a difference – Chris Curtis Feb 20 '15 at 18:48
  • Figured out what was wrong in my case - the connection string. Removed meta and UnintentionalCodeFirstException's. It works now. Thank you! – SergB Feb 26 '15 at 06:09