I have an application that uses two separate models stored in a single database. The first model is set up with migrations and is the one that has created the migrations data in the database. The second is a very simple model that needs no model validation at all - the tables it uses exist and are of the proper structure. The second Context works fine in a separate database with the same table structure.
The problem is it fails when running in the same database with the first model since it does provide some sort of model validation. It complains that the context has changed since the last update, but of course the the migrations data does not contain anything about the second context's tables.
Is it possible to turn off the meta data validation for the context, and just let the second context work against the tables as is, since I know that works?
in the context constructor but that has no effect.
The solution is to use implement a "do nothing" database initializer that basically does nothing.
public class QueueMessageManagerContextInitializer : IDatabaseInitializer<QueueMessageManagerContext>
{
protected void Seed(QueueMessageManagerContext context)
{
}
public void InitializeDatabase(QueueMessageManagerContext context)
{
// do nothing
Seed(context);
}
}
To use in one time startup code then:
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
//Database.SetInitializer<QueueMessageManagerContext>(new DropCreateDatabaseIfModelChanges<QueueMessageManagerContext>());
Database.SetInitializer<QueueMessageManagerContext>(new QueueMessageManagerContextInitializer());
}
Simple but non-obvious solution.
Edit:
Even simpler solution: Just pass NULL to the SetInitializer() method:
Database.SetInitializer<QueueMessageManagerContext>(null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With