-1

Is it possible, in a code-first approach, to define two different context classes and use them simultaneously?

The database schema is already given and I cannot modify it (besides the creation of some new tables).

The problem I'm facing is that I'm getting "The model backing... has changed" error. It seems there's some kind of collision between the two contexts.

Note that each context includes a different set of tables, so sync problems aren't a concern.

sapito
  • 1,472
  • 3
  • 17
  • 26
  • WHEN exactly you are getting the error? – e4rthdog Sep 27 '14 at 14:13
  • As soon as I instantiate two contexts, or just one context that isn't the same instantiated in a previous execution. – sapito Sep 27 '14 at 14:19
  • From the package manager console , what do you get if you run a test migration? Does it show anything in the up or down methods? `Add-Migration TestMigration` – e4rthdog Sep 27 '14 at 14:48
  • 1
    You should set Database.SetInitializer(null); in Global.asax to prevent EF6 trying to sync the db schema with your model. – Faris Zacina Sep 27 '14 at 15:03
  • Also check out this thread: http://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea – Faris Zacina Sep 27 '14 at 15:03

1 Answers1

0

Assuming you are using EF 6 or higher, this can be done either by the ContextKey-Property of DbMigrationsConfiguration (this will make the MigrationsHistory table multi tenant by ContextKey) or by setting HasDefaultSchema in OnModelCreating (this will enable multiple MigrationsHistory tables, one for each schema). For further instructions see the Documentation.

user3411327
  • 1,031
  • 8
  • 14