How to exclude one table from automatic code first migrations in the Entity Framework?

My TEMPORARY solution, only for dev environments. I have a separate script that runs migration and program run does not check them. So in unexpected case I was possible to invoke Ignore<ContactView>() and run migrations with this line. When it was completed, I removed this line!

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // comment out this code after migrations are done
        modelBuilder.Ignore<ContactView>();
    }

It is possible by using another DbContext to access the table in question. Migrations are bound to one DbContext (see Is it possible to have automatic migrations for one DbContext and not for another in the same project?).


Not sure if this is the OP's exact scenario, but I had a table that I did not want a migration generated for. I accomplished this by using ToView instead of ToTable within the DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<MyTable>(entity => {
        // Migration will not be generated for this table
        entity.ToView("MyTable", "dbo");

        entity.Property(e => e.FooBar).HasColumnType("DECIMAL(19,9)");
    });
}

It feels a bit hacky to me, but maybe it's not -- because, after all, I'm just trying to "view" the table, not write to it...

[Tested with .NET Core EF 3.1.3]