Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to have DbContext Ignore Migration/Version data in Database?

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.

like image 779
Rick Strahl Avatar asked Mar 26 '13 04:03

Rick Strahl


1 Answers

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);
like image 132
Rick Strahl Avatar answered Dec 07 '22 09:12

Rick Strahl