0

I have two databases that have almost the same table names, the difference is that every table name is appended with a two digit number:

So I have:

Database name:       |Database01  |       |Database02  |
                     |------------|       |------------|
                     | Customer01 |       | Customer02 |
Tables:              |Inventory01 |       |Inventory02 |

I'm using entity framework power tools in order to reverse engineer the databases. The number of databases is likely to increase so re-creating the models every time a database is added is not an option.

So what I'm trying to do is to 're-use' the same context, and only to change the "Initial Catalog" variable of the connection string in the App.config and also changing the table name with:

public public class MyContext: Database02
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Client01>().ToTable("Client02");
    }
}

But it throws an exception:

"The model backing the 'MyContext' context has changed since the database was     
created. Consider using Code First Migrations to update the database 
(http://go.microsoft.com/fwlink/?LinkId=238269)."}

I did see a similar question here in SO but it is no longer relevant in EF 6.0

UPDATE: The following code doesn't fix the problem:

 public class Database02: DBContext
 {
    static Database02()
    {
        Database.SetInitializer<Database02>(null);
    }
 }

How can I solve this issue without having to recreate the model every time a new database is created?

What option do I have to achieve this. I don't exactly see how Code First Migration link given in the exception would help me?

Community
  • 1
  • 1
user3105717
  • 121
  • 6

1 Answers1

0

Well I got it working seems I somehow missed to disable the database initializer in my derived context (MyContext). It worked if I add an static constructor to MyContext class (so it gets called only once and at the first time I use MyContext):

public public class MyContext: Database02
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Client01>().ToTable("Client02");
    }

    // This static method was missing, it is required to disable the db initializer
    static MyContext()
    {
        Database.SetInitializer<MyContext>(null);
    }
user3105717
  • 121
  • 6