Is it possible to have a DbContext ignore migration / version data in a database?

I have an application that uses two separate models stored in one database. The first model is configured with migrations and is the one that created the migration data in the database. The second is a very simple model that does not need to be tested at all - the tables it uses have and have the proper structure. The second Context works fine in a separate database with the same table structure.

The problem is that it does not work when working in the same database with the first model, since it provides some verification of the model. He complains that the context has changed since the last update, but, of course, the migration data does not contain anything about the second context tables.

Is it possible to disable metadata validation for the context and just let the second context work with the tables as is, since I know that this works?

in the context constructor, but this has no effect.

+7
source share
1 answer

The solution is to use the do nothing database initializer implementation, which basically does nothing.

public class QueueMessageManagerContextInitializer : IDatabaseInitializer<QueueMessageManagerContext> { protected void Seed(QueueMessageManagerContext context) { } public void InitializeDatabase(QueueMessageManagerContext context) { // do nothing Seed(context); } } 

To use the startup code at one time, follow these steps:

  [ClassInitialize()] public static void MyClassInitialize(TestContext testContext) { //Database.SetInitializer<QueueMessageManagerContext>(new DropCreateDatabaseIfModelChanges<QueueMessageManagerContext>()); Database.SetInitializer<QueueMessageManagerContext>(new QueueMessageManagerContextInitializer()); } 

A simple but unobvious solution.

Edit:

An even simpler solution: just pass NULL to the SetInitializer () method:

 Database.SetInitializer<QueueMessageManagerContext>(null); 
+10
source

All Articles